/* []----------------------------------------------[] | Constraint.cpp | []----------------------------------------------[] | | | AUTHOR: MFSomers 2005. | | USE: Defines a constraint framework | | class to be used for different | | equations of motions | | integrators... | | | []----------------------------------------------[] */ // Copyright (C) 2005 M.F. Somers, Theoretical Chemistry Department, Leiden University // // This is free software; you can redistribute it and/or modify it under the terms of // the GNU Lesser General Public License as published by the Free Software Foundation. // // http://www.gnu.org/licenses/lgpl.txt #include "Global.h" #include "Vector.h" #include "Constants.h" #include "ClassicalDynamics.h" /* --------------------------------------------------- Constraint ------- */ Constraint::Constraint( unsigned int TheId ) : ParticleIDs( 2 ), ParticleIndices( 2 ) { Id = TheId; Type = AGeneralConstraint; TheInternalFlag = 0; ParticleIDs[ 0 ] = ParticleIDs[ 1 ] = 0; ParticleIndices[ 0 ] = ParticleIndices[ 1 ] = -1; } /* ---------------------------------------------------------------------- */ Constraint::Constraint( Constraint& C ) : ParticleIDs( C.ParticleIDs.size() ), ParticleIndices( C.ParticleIDs.size() ) { Id = C.Id; Type = C.Type; TheInternalFlag = C.TheInternalFlag; for( int N = 0; N < C.ParticleIDs.size(); ++N ) { ParticleIDs[ N ] = C.ParticleIDs[ N ]; ParticleIndices[ N ] = C.ParticleIndices[ N ]; } } /* ---------------------------------------------------------------------- */ Constraint& Constraint::operator=( Constraint& C ) { int N, n; if( &C == &(*this) ) return( (*this) ); Id = C.Id; TheInternalFlag = C.TheInternalFlag; Type = C.Type; N = C.ParticleIDs.size() - ParticleIDs.size(); if( N > 0 ) ParticleIDs.insert( ParticleIDs.end(), N, 0 ); N = C.ParticleIDs.size() - ParticleIndices.size(); if( N > 0 ) ParticleIndices.insert( ParticleIndices.end(), N, -1 ); for( n = 0; n < C.ParticleIDs.size(); ++n ) // copy tables ParticleIDs[ n ] = C.ParticleIDs[ n ]; for( ; n < ParticleIDs.size(); ++n ) ParticleIDs[ n ] = 0; for( n = 0; n < C.ParticleIndices.size(); ++n ) ParticleIndices[ n ] = C.ParticleIndices[ n ]; for( ; n < ParticleIndices.size(); ++n ) ParticleIndices[ n ] = -1; return( (*this) ); } /* ---------------------------------------------------------------------- */ void Constraint::IncludeParticle( Particle& P ) { int EmptySpot, Pos; for( EmptySpot = -1, Pos = 0; Pos < ParticleIDs.size(); ++Pos ) { if( ParticleIDs[ Pos ] == 0 ) { if( EmptySpot <= -1 ) EmptySpot = Pos; } else if( ParticleIDs[ Pos ] == P.Id ) return; } if( EmptySpot <= -1 ) { ParticleIDs.insert( ParticleIDs.end(), 4, 0 ); ParticleIndices.insert( ParticleIndices.end(), 4, -1 ); EmptySpot = Pos; } ParticleIDs[ EmptySpot ] = P.Id; ParticleIndices[ EmptySpot ] = -1; } /* ---------------------------------------------------------------------- */ void Constraint::ExcludeParticle( unsigned int ID ) { int Index = IsParticleIncluded( ID ); if( Index >= 0 ) { ParticleIDs[ Index ] = 0; ParticleIndices[ Index ] = -1; } } /* ---------------------------------------------------------------------- */ void Constraint::ExcludeParticle( Particle& P ) { ExcludeParticle( P.Id ); } /* ---------------------------------------------------------------------- */ int Constraint::IsParticleIncluded( unsigned int ID ) { for( int n = 0; n < ParticleIDs.size(); ++n ) if( ParticleIDs[ n ] == ID ) return( n ); return( -1 ); } /* ---------------------------------------------------------------------- */ int Constraint::IsParticleIncluded( Particle& P ) { return( IsParticleIncluded( P.Id ) ); } /* ---------------------------------------------------------------------- */ unsigned int Constraint::GetParticleId( int Index ) { return( ( Index < 0 ? 0 : ( Index >= ParticleIDs.size() ? 0 : ParticleIDs[ Index ] ) ) ); } /* ---------------------------------------------------------------------- */ void Constraint::ExcludeAllParticles( void ) { for( int n = 0; n < ParticleIDs.size(); ++n ) { ParticleIDs[ n ] = 0; ParticleIndices[ n ] = -1; } } /* ---------------------------------------------------------------------- */ int Constraint::NrOfParticlesIncluded( void ) { int n, Count; for( Count = n = 0; n < ParticleIDs.size(); ++n ) if( ParticleIDs[ n ] ) ++Count; return( Count ); } /* ---------------------------------------------------------------------- */ int Constraint::GetParticleListSize( void ) { return( ParticleIDs.size() ); } /* ---------------------------------------------------------------------- */