예제 #1
0
		void Integrator::Minimize( const unsigned int max, unsigned int &simpleSteps, unsigned int &quadraticSteps ) {
			const double eigStore = maxEigenvalue;
			if( !mParameters.ShouldProtoMolDiagonalize && eigenvectors.size() == 0 ) {
				computeProjectionVectors();
			}

			SaveStep();

			double initialPE = context->calcForcesAndEnergy( true, true );
			( ( StepKernel & )( kernel.getImpl() ) ).setOldPositions();

			//context->getPositions(oldPos); // I need to get old positions here
			simpleSteps = 0;
			quadraticSteps = 0;

			for( unsigned int i = 0; i < max; i++ ) {
				SetProjectionChanged( false );

				if( initialPE < mMetropolisPE ) {
					break;
				}

				const double prob = exp(-( 1.0 / ( BOLTZ * temperature )) * ( initialPE - mMetropolisPE ));
				if( SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber() < prob ) {
					break;
				}

				simpleSteps++;
				double currentPE = LinearMinimize( initialPE );
				if( mParameters.isAlwaysQuadratic || currentPE > initialPE ) {
					quadraticSteps++;

					double lambda = 0.0;
					currentPE = QuadraticMinimize( currentPE, lambda );
					if( currentPE > initialPE ) {
						std::cout << "Quadratic Minimization Failed Energy Test [" << currentPE << ", " << initialPE << "] - Forcing Rediagonalization" << std::endl;
						if( !mParameters.ShouldProtoMolDiagonalize ) computeProjectionVectors();
						break;
					} else {
						if( mParameters.ShouldForceRediagOnQuadraticLambda && lambda < 1.0 / maxEigenvalue ) {
							std::cout << "Quadratic Minimization Failed Lambda Test [" << lambda << ", " << 1.0 / maxEigenvalue << "] - Forcing Rediagonalization" << std::endl;
							if( !mParameters.ShouldProtoMolDiagonalize ) computeProjectionVectors();
							break;
						}
					}
				}

				if( mParameters.ShouldUseMetropolisMinimization ) {
					if( currentPE < mMetropolisPE ) {
						break;
					}

					const double prob = exp(-( 1.0 / ( BOLTZ * temperature )) * ( currentPE - mMetropolisPE ));
					if( SimTKOpenMMUtilities::getUniformlyDistributedRandomNumber() < prob ) {
						break;
					}
				} else {
					const double diff = initialPE - currentPE;
					if( diff < getMinimumLimit() && diff >= 0.0 ) {
						break;
					}

					if( diff > 0.0 ) {
						SaveStep();
						initialPE = currentPE;
					} else {
						RevertStep();
						context->calcForcesAndEnergy( true, false );

						maxEigenvalue *= 2;
					}
				}
			}

			mSimpleMinimizations += simpleSteps;
			mQuadraticMinimizations += quadraticSteps;

			maxEigenvalue = eigStore;
		}
예제 #2
0
void SeekPath(int stepleft)
{
	DuplicatePos(stepleft, 'b');
	int i = 0, j = 0, k = 0, l = 0, x = 0, y = 0, activeCount = 0;
	//int o, p;//more spaces involved
	for (x = 0;x <= y;x++)
	{
		DuplicatePos(stepleft + maxstep, 'b');
		Compare(x, 0, stepleft, 1);
		if (stepleft > 0)
		{
			DuplicatePos((stepleft + maxstep + maxstep), 'b');
			for (i = 1;i <= maxlength;i++)
			{
				for (j = 1;j <= (int)pow(2, i);j++)
				{
					if (pos[0][i][j] == 0)
						continue;
					else
					{
						Detach(i, j);
						DuplicatePos(stepleft + maxstep + maxstep + maxstep, 'b');
						for (k = 1;k <= maxlength;k++)//k = 0 need to examine
						{
							for (l = 1;l <= (int)pow(2, k);l++)
							{
								if (pos[0][k][l] < 1 && pos[0][k - 1][(l + 1) / 2] >= 1)
								{
									if (l % 2 == 1 && pos[0][k][l] == 0)
									{
										Attach(k, l);
										//printf("\n1step%d %d,%d ---> %d,%d:\n", maxstep + 1 - stepleft, i, j, k, l);
										//Display();
										if (stepleft > 1)
										{
											SaveStep(i, j, k, l, x, stepleft);
											Compare(x, 0, stepleft, 0);
											//printf("1");
											SeekPath(stepleft - 1);
										}
										else
										{
											activeCount = Active(1);
											SaveStep(i, j, k, l, x, stepleft);
											Compare(x, activeCount, stepleft, 0);
											//printf("1");
										}
									}
									else if (l % 2 == 0 && pos[0][k][l - 1] >= 1 && pos[0][k][l - 1] != 2 && pos[1][0][1] != 2)
									{
										if (pos[0][k][l] == -1)//case 1 -1 ---> 1  1
										{
											SideSwitch(k, l - 1);
											Attach(k, l - 1);
											//printf("\n2step%d %d,%d ---> %d,%d:\n", maxstep + 1 - stepleft, i, j, k, l - 1);
											//Display();
											if (stepleft > 1)
											{
												SaveStep(i, j, k, l - 1, x, stepleft);
												Compare(x, 0, stepleft, 0);
												//printf("2");
												SeekPath(stepleft - 1);
											}
											else
											{
												activeCount = Active(1);
												SaveStep(i, j, k, l - 1, x, stepleft);
												Compare(x, activeCount, stepleft, 0);
												//printf("2");
											}
										}
										else if (pos[0][k][l] == -2)//case 1 -2 ---> 1  1
										{
											Attach(k, l);
											//printf("\n3step%d %d,%d ---> %d,%d:\n", maxstep + 1 - stepleft, i, j, k, l);
											//Display();
											if (stepleft > 1)
											{
												SaveStep(i, j, k, l, x, stepleft);
												Compare(x, 0, stepleft, 0);
												//printf("3");
												SeekPath(stepleft - 1);
											}
											else
											{
												activeCount = Active(1);
												SaveStep(i, j, k, l, x, stepleft);
												Compare(x, activeCount, stepleft, 0);
												//printf("3");
											}
										}
										else if (pos[0][k][l] == 0)//case 1  0 ---> 1  1 ,two ways to attach, left and right
										{
											//attach left
											SideSwitch(k, l - 1);
											Attach(k, l - 1);
											//printf("\n4step%d %d,%d ---> %d,%d:\n", maxstep + 1 - stepleft, i, j, k, l - 1);
											//Display();
											if (stepleft > 1)
											{
												SaveStep(i, j, k, l - 1, x, stepleft);
												Compare(x, 0, stepleft, 0);
												//printf("4");
												SeekPath(stepleft - 1);
											}
											else
											{
												activeCount = Active(1);
												SaveStep(i, j, k, l - 1, x, stepleft);
												Compare(x, activeCount, stepleft, 0);
												//printf("4");
											}
											DuplicatePos(stepleft + maxstep + maxstep + maxstep, 'r');

											//attach right
											Attach(k, l);
											//printf("\n5step%d %d,%d ---> %d,%d:\n", maxstep + 1 - stepleft, i, j, k, l);
											//Display();
											if (stepleft > 1)
											{
												SaveStep(i, j, k, l, x, stepleft);
												Compare(x, 0, stepleft, 0);
												//printf("5");
												SeekPath(stepleft - 1);
											}
											else
											{
												activeCount = Active(1);
												SaveStep(i, j, k, l, x, stepleft);
												Compare(x, activeCount, stepleft, 0);
												//printf("5");
											}
										}
									}
									DuplicatePos(stepleft + maxstep + maxstep + maxstep, 'r');
								}
							}
						}
						DuplicatePos(stepleft + maxstep + maxstep, 'r');
					}
				}
			}
		}
		DuplicatePos(stepleft + maxstep, 'r');
		y += Active(0);
	}
}