--- trunk/OOPSE/libmdtools/Integrator.cpp 2003/08/26 20:13:17 724 +++ trunk/OOPSE/libmdtools/Integrator.cpp 2003/08/26 20:29:26 725 @@ -11,8 +11,8 @@ template Integrator::Integrator( SimInf #include "simError.h" -template Integrator::Integrator( SimInfo *theInfo, ForceFields* the_ff ) { - +template Integrator::Integrator(SimInfo* theInfo, + ForceFields* the_ff){ info = theInfo; myFF = the_ff; isFirst = 1; @@ -21,29 +21,30 @@ template Integrator::Integrator( SimInf nMols = info->n_mol; // give a little love back to the SimInfo object - - if( info->the_integrator != NULL ) delete info->the_integrator; + + if (info->the_integrator != NULL){ + delete info->the_integrator; + } info->the_integrator = this; nAtoms = info->n_atoms; // check for constraints - - constrainedA = NULL; - constrainedB = NULL; + + constrainedA = NULL; + constrainedB = NULL; constrainedDsqr = NULL; - moving = NULL; - moved = NULL; - oldPos = NULL; - + moving = NULL; + moved = NULL; + oldPos = NULL; + nConstrained = 0; checkConstraints(); } -template Integrator::~Integrator() { - - if( nConstrained ){ +template Integrator::~Integrator(){ + if (nConstrained){ delete[] constrainedA; delete[] constrainedB; delete[] constrainedDsqr; @@ -51,117 +52,106 @@ template Integrator::~Integrator() { delete[] moved; delete[] oldPos; } - } -template void Integrator::checkConstraints( void ){ - - +template void Integrator::checkConstraints(void){ isConstrained = 0; - Constraint *temp_con; - Constraint *dummy_plug; + Constraint* temp_con; + Constraint* dummy_plug; temp_con = new Constraint[info->n_SRI]; nConstrained = 0; int constrained = 0; - + SRI** theArray; - for(int i = 0; i < nMols; i++){ - - theArray = (SRI**) molecules[i].getMyBonds(); - for(int j=0; jis_constrained(); - if(constrained){ + if (constrained){ + dummy_plug = theArray[j]->get_constraint(); + temp_con[nConstrained].set_a(dummy_plug->get_a()); + temp_con[nConstrained].set_b(dummy_plug->get_b()); + temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr()); - dummy_plug = theArray[j]->get_constraint(); - temp_con[nConstrained].set_a( dummy_plug->get_a() ); - temp_con[nConstrained].set_b( dummy_plug->get_b() ); - temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() ); - - nConstrained++; - constrained = 0; - } + nConstrained++; + constrained = 0; + } } - theArray = (SRI**) molecules[i].getMyBends(); - for(int j=0; jis_constrained(); - - if(constrained){ - - dummy_plug = theArray[j]->get_constraint(); - temp_con[nConstrained].set_a( dummy_plug->get_a() ); - temp_con[nConstrained].set_b( dummy_plug->get_b() ); - temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() ); - - nConstrained++; - constrained = 0; + + if (constrained){ + dummy_plug = theArray[j]->get_constraint(); + temp_con[nConstrained].set_a(dummy_plug->get_a()); + temp_con[nConstrained].set_b(dummy_plug->get_b()); + temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr()); + + nConstrained++; + constrained = 0; } } - theArray = (SRI**) molecules[i].getMyTorsions(); - for(int j=0; jis_constrained(); - - if(constrained){ - - dummy_plug = theArray[j]->get_constraint(); - temp_con[nConstrained].set_a( dummy_plug->get_a() ); - temp_con[nConstrained].set_b( dummy_plug->get_b() ); - temp_con[nConstrained].set_dsqr( dummy_plug->get_dsqr() ); - - nConstrained++; - constrained = 0; + + if (constrained){ + dummy_plug = theArray[j]->get_constraint(); + temp_con[nConstrained].set_a(dummy_plug->get_a()); + temp_con[nConstrained].set_b(dummy_plug->get_b()); + temp_con[nConstrained].set_dsqr(dummy_plug->get_dsqr()); + + nConstrained++; + constrained = 0; } } } - if(nConstrained > 0){ - + if (nConstrained > 0){ isConstrained = 1; - if(constrainedA != NULL ) delete[] constrainedA; - if(constrainedB != NULL ) delete[] constrainedB; - if(constrainedDsqr != NULL ) delete[] constrainedDsqr; + if (constrainedA != NULL) + delete[] constrainedA; + if (constrainedB != NULL) + delete[] constrainedB; + if (constrainedDsqr != NULL) + delete[] constrainedDsqr; - constrainedA = new int[nConstrained]; - constrainedB = new int[nConstrained]; + constrainedA = new int[nConstrained]; + constrainedB = new int[nConstrained]; constrainedDsqr = new double[nConstrained]; - - for( int i = 0; i < nConstrained; i++){ - + + for (int i = 0; i < nConstrained; i++){ constrainedA[i] = temp_con[i].get_a(); constrainedB[i] = temp_con[i].get_b(); constrainedDsqr[i] = temp_con[i].get_dsqr(); - } - + // save oldAtoms to check for lode balanceing later on. - + oldAtoms = nAtoms; - + moving = new int[nAtoms]; - moved = new int[nAtoms]; + moved = new int[nAtoms]; - oldPos = new double[nAtoms*3]; + oldPos = new double[nAtoms * 3]; } - + delete[] temp_con; } -template void Integrator::integrate( void ){ - +template void Integrator::integrate(void){ int i, j; // loop counters - double runTime = info->run_time; - double sampleTime = info->sampleTime; - double statusTime = info->statusTime; + double runTime = info->run_time; + double sampleTime = info->sampleTime; + double statusTime = info->statusTime; double thermalTime = info->thermalTime; double currSample; @@ -171,9 +161,9 @@ template void Integrator::integrate( vo int calcPot, calcStress; int isError; - tStats = new Thermo( info ); - statOut = new StatWriter( info ); - dumpOut = new DumpWriter( info ); + tStats = new Thermo(info); + statOut = new StatWriter(info); + dumpOut = new DumpWriter(info); atoms = info->atoms; DirectionalAtom* dAtom; @@ -185,65 +175,67 @@ template void Integrator::integrate( vo calcForce(1, 1); // myFF->doForces(1,1); - - if( info->setTemp ){ - + + if (info->setTemp){ thermalize(); } + + calcPot = 0; + calcStress = 0; + currSample = sampleTime; + currThermal = thermalTime; + currStatus = statusTime; calcPot = 0; calcStress = 0; currSample = sampleTime + info->getTime(); currThermal = thermalTime+ info->getTime(); currStatus = statusTime + info->getTime(); +>>>>>>> 1.18 - dumpOut->writeDump( info->getTime() ); - statOut->writeStat( info->getTime() ); + dumpOut->writeDump(info->getTime()); + statOut->writeStat(info->getTime()); readyCheck(); #ifdef IS_MPI - strcpy( checkPointMsg, - "The integrator is ready to go." ); + strcpy(checkPointMsg, "The integrator is ready to go."); MPIcheckPoint(); #endif // is_mpi - while( info->getTime() < runTime ){ - - if( (info->getTime()+dt) >= currStatus ){ + while (info->getTime() < runTime){ + if ((info->getTime() + dt) >= currStatus){ calcPot = 1; calcStress = 1; } - integrateStep( calcPot, calcStress ); - + integrateStep(calcPot, calcStress); + info->incrTime(dt); - if( info->setTemp ){ - if( info->getTime() >= currThermal ){ - thermalize(); - currThermal += thermalTime; + if (info->setTemp){ + if (info->getTime() >= currThermal){ + thermalize(); + currThermal += thermalTime; } } - if( info->getTime() >= currSample ){ - dumpOut->writeDump( info->getTime() ); + if (info->getTime() >= currSample){ + dumpOut->writeDump(info->getTime()); currSample += sampleTime; } - if( info->getTime() >= currStatus ){ - statOut->writeStat( info->getTime() ); + if (info->getTime() >= currStatus){ + statOut->writeStat(info->getTime()); calcPot = 0; calcStress = 0; currStatus += statusTime; } #ifdef IS_MPI - strcpy( checkPointMsg, - "successfully took a time step." ); + strcpy(checkPointMsg, "successfully took a time step."); MPIcheckPoint(); #endif // is_mpi - } dumpOut->writeFinal(info->getTime()); @@ -252,49 +244,50 @@ template void Integrator::integrateStep delete statOut; } -template void Integrator::integrateStep( int calcPot, int calcStress ){ - - - +template void Integrator::integrateStep(int calcPot, + int calcStress){ // Position full step, and velocity half step - preMove(); + moveA(); - if( nConstrained ) constrainA(); - + if (nConstrained){ + constrainA(); + } + + #ifdef IS_MPI - strcpy( checkPointMsg, "Succesful moveA\n" ); + strcpy(checkPointMsg, "Succesful moveA\n"); MPIcheckPoint(); #endif // is_mpi - + // calc forces - calcForce(calcPot,calcStress); + calcForce(calcPot, calcStress); #ifdef IS_MPI - strcpy( checkPointMsg, "Succesful doForces\n" ); + strcpy(checkPointMsg, "Succesful doForces\n"); MPIcheckPoint(); #endif // is_mpi - + // finish the velocity half step - + moveB(); - if( nConstrained ) constrainB(); - + + if (nConstrained){ + constrainB(); + } + #ifdef IS_MPI - strcpy( checkPointMsg, "Succesful moveB\n" ); + strcpy(checkPointMsg, "Succesful moveB\n"); MPIcheckPoint(); #endif // is_mpi - - } -template void Integrator::moveA( void ){ - +template void Integrator::moveA(void){ int i, j; DirectionalAtom* dAtom; double Tb[3], ji[3]; @@ -303,138 +296,129 @@ template void Integrator::moveA( void ) double vel[3], pos[3], frc[3]; double mass; - for( i=0; igetVel(vel); + atoms[i]->getPos(pos); + atoms[i]->getFrc(frc); - atoms[i]->getVel( vel ); - atoms[i]->getPos( pos ); - atoms[i]->getFrc( frc ); - mass = atoms[i]->getMass(); - for (j=0; j < 3; j++) { + for (j = 0; j < 3; j++){ // velocity half step - vel[j] += ( dt2 * frc[j] / mass ) * eConvert; + vel[j] += (dt2 * frc[j] / mass) * eConvert; // position whole step pos[j] += dt * vel[j]; } - atoms[i]->setVel( vel ); - atoms[i]->setPos( pos ); + atoms[i]->setVel(vel); + atoms[i]->setPos(pos); - if( atoms[i]->isDirectional() ){ + if (atoms[i]->isDirectional()){ + dAtom = (DirectionalAtom *) atoms[i]; - dAtom = (DirectionalAtom *)atoms[i]; - // get and convert the torque to body frame - - dAtom->getTrq( Tb ); - dAtom->lab2Body( Tb ); + dAtom->getTrq(Tb); + dAtom->lab2Body(Tb); + // get the angular momentum, and propagate a half step - dAtom->getJ( ji ); + dAtom->getJ(ji); - for (j=0; j < 3; j++) + for (j = 0; j < 3; j++) ji[j] += (dt2 * Tb[j]) * eConvert; - + // use the angular velocities to propagate the rotation matrix a // full time step dAtom->getA(A); dAtom->getI(I); - + // rotate about the x-axis angle = dt2 * ji[0] / I[0][0]; - this->rotate( 1, 2, angle, ji, A ); + this->rotate(1, 2, angle, ji, A); // rotate about the y-axis angle = dt2 * ji[1] / I[1][1]; - this->rotate( 2, 0, angle, ji, A ); - + this->rotate(2, 0, angle, ji, A); + // rotate about the z-axis angle = dt * ji[2] / I[2][2]; - this->rotate( 0, 1, angle, ji, A); - + this->rotate(0, 1, angle, ji, A); + // rotate about the y-axis angle = dt2 * ji[1] / I[1][1]; - this->rotate( 2, 0, angle, ji, A ); - - // rotate about the x-axis + this->rotate(2, 0, angle, ji, A); + + // rotate about the x-axis angle = dt2 * ji[0] / I[0][0]; - this->rotate( 1, 2, angle, ji, A ); - + this->rotate(1, 2, angle, ji, A); - dAtom->setJ( ji ); - dAtom->setA( A ); - - } + + dAtom->setJ(ji); + dAtom->setA(A); + } } } -template void Integrator::moveB( void ){ +template void Integrator::moveB(void){ int i, j; DirectionalAtom* dAtom; double Tb[3], ji[3]; double vel[3], frc[3]; double mass; - for( i=0; igetVel( vel ); - atoms[i]->getFrc( frc ); + for (i = 0; i < nAtoms; i++){ + atoms[i]->getVel(vel); + atoms[i]->getFrc(frc); mass = atoms[i]->getMass(); // velocity half step - for (j=0; j < 3; j++) - vel[j] += ( dt2 * frc[j] / mass ) * eConvert; - - atoms[i]->setVel( vel ); - - if( atoms[i]->isDirectional() ){ + for (j = 0; j < 3; j++) + vel[j] += (dt2 * frc[j] / mass) * eConvert; - dAtom = (DirectionalAtom *)atoms[i]; + atoms[i]->setVel(vel); + if (atoms[i]->isDirectional()){ + dAtom = (DirectionalAtom *) atoms[i]; + // get and convert the torque to body frame - dAtom->getTrq( Tb ); - dAtom->lab2Body( Tb ); + dAtom->getTrq(Tb); + dAtom->lab2Body(Tb); // get the angular momentum, and propagate a half step - dAtom->getJ( ji ); + dAtom->getJ(ji); - for (j=0; j < 3; j++) + for (j = 0; j < 3; j++) ji[j] += (dt2 * Tb[j]) * eConvert; - - dAtom->setJ( ji ); + + dAtom->setJ(ji); } } } -template void Integrator::preMove( void ){ +template void Integrator::preMove(void){ int i, j; double pos[3]; - if( nConstrained ){ + if (nConstrained){ + for (i = 0; i < nAtoms; i++){ + atoms[i]->getPos(pos); - for(i=0; i < nAtoms; i++) { - - atoms[i]->getPos( pos ); - - for (j = 0; j < 3; j++) { - oldPos[3*i + j] = pos[j]; + for (j = 0; j < 3; j++){ + oldPos[3 * i + j] = pos[j]; } - } - } + } } template void Integrator::constrainA(){ - - int i,j,k; + int i, j, k; int done; double posA[3], posB[3]; double velA[3], velB[3]; @@ -449,122 +433,117 @@ template void Integrator::constrainA(){ double gab; int iteration; - for( i=0; igetPos( posA ); - atoms[b]->getPos( posB ); - - for (j = 0; j < 3; j++ ) + bx = (b * 3) + 0; + by = (b * 3) + 1; + bz = (b * 3) + 2; + + if (moved[a] || moved[b]){ + atoms[a]->getPos(posA); + atoms[b]->getPos(posB); + + for (j = 0; j < 3; j++) pab[j] = posA[j] - posB[j]; - - //periodic boundary condition - info->wrapVector( pab ); + //periodic boundary condition - pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2]; + info->wrapVector(pab); - rabsq = constrainedDsqr[i]; - diffsq = rabsq - pabsq; + pabsq = pab[0] * pab[0] + pab[1] * pab[1] + pab[2] * pab[2]; - // the original rattle code from alan tidesley - if (fabs(diffsq) > (tol*rabsq*2)) { - rab[0] = oldPos[ax] - oldPos[bx]; - rab[1] = oldPos[ay] - oldPos[by]; - rab[2] = oldPos[az] - oldPos[bz]; + rabsq = constrainedDsqr[i]; + diffsq = rabsq - pabsq; - info->wrapVector( rab ); + // the original rattle code from alan tidesley + if (fabs(diffsq) > (tol * rabsq * 2)){ + rab[0] = oldPos[ax] - oldPos[bx]; + rab[1] = oldPos[ay] - oldPos[by]; + rab[2] = oldPos[az] - oldPos[bz]; - rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2]; + info->wrapVector(rab); - rpabsq = rpab * rpab; + rpab = rab[0] * pab[0] + rab[1] * pab[1] + rab[2] * pab[2]; + rpabsq = rpab * rpab; - if (rpabsq < (rabsq * -diffsq)){ + if (rpabsq < (rabsq * -diffsq)){ #ifdef IS_MPI - a = atoms[a]->getGlobalIndex(); - b = atoms[b]->getGlobalIndex(); + a = atoms[a]->getGlobalIndex(); + b = atoms[b]->getGlobalIndex(); #endif //is_mpi - sprintf( painCave.errMsg, - "Constraint failure in constrainA at atom %d and %d.\n", - a, b ); - painCave.isFatal = 1; - simError(); - } + sprintf(painCave.errMsg, + "Constraint failure in constrainA at atom %d and %d.\n", a, + b); + painCave.isFatal = 1; + simError(); + } - rma = 1.0 / atoms[a]->getMass(); - rmb = 1.0 / atoms[b]->getMass(); + rma = 1.0 / atoms[a]->getMass(); + rmb = 1.0 / atoms[b]->getMass(); - gab = diffsq / ( 2.0 * ( rma + rmb ) * rpab ); + gab = diffsq / (2.0 * (rma + rmb) * rpab); dx = rab[0] * gab; dy = rab[1] * gab; dz = rab[2] * gab; - posA[0] += rma * dx; - posA[1] += rma * dy; - posA[2] += rma * dz; + posA[0] += rma * dx; + posA[1] += rma * dy; + posA[2] += rma * dz; - atoms[a]->setPos( posA ); + atoms[a]->setPos(posA); - posB[0] -= rmb * dx; - posB[1] -= rmb * dy; - posB[2] -= rmb * dz; + posB[0] -= rmb * dx; + posB[1] -= rmb * dy; + posB[2] -= rmb * dz; - atoms[b]->setPos( posB ); + atoms[b]->setPos(posB); dx = dx / dt; dy = dy / dt; dz = dz / dt; - atoms[a]->getVel( velA ); + atoms[a]->getVel(velA); - velA[0] += rma * dx; - velA[1] += rma * dy; - velA[2] += rma * dz; + velA[0] += rma * dx; + velA[1] += rma * dy; + velA[2] += rma * dz; - atoms[a]->setVel( velA ); + atoms[a]->setVel(velA); - atoms[b]->getVel( velB ); + atoms[b]->getVel(velB); - velB[0] -= rmb * dx; - velB[1] -= rmb * dy; - velB[2] -= rmb * dz; + velB[0] -= rmb * dx; + velB[1] -= rmb * dy; + velB[2] -= rmb * dz; - atoms[b]->setVel( velB ); + atoms[b]->setVel(velB); - moving[a] = 1; - moving[b] = 1; - done = 0; - } + moving[a] = 1; + moving[b] = 1; + done = 0; + } } } - - for(i=0; i void Integrator::constrainA(){ iteration++; } - if( !done ){ - - sprintf( painCave.errMsg, - "Constraint failure in constrainA, too many iterations: %d\n", - iteration ); + if (!done){ + sprintf(painCave.errMsg, + "Constraint failure in constrainA, too many iterations: %d\n", + iteration); painCave.isFatal = 1; simError(); } - } -template void Integrator::constrainB( void ){ - - int i,j,k; +template void Integrator::constrainB(void){ + int i, j, k; int done; double posA[3], posB[3]; double velA[3], velB[3]; @@ -599,103 +575,96 @@ template void Integrator::constrainB( v double gab; int iteration; - for(i=0; igetVel(velA); + atoms[b]->getVel(velB); - atoms[a]->getVel( velA ); - atoms[b]->getVel( velB ); - - vxab = velA[0] - velB[0]; - vyab = velA[1] - velB[1]; - vzab = velA[2] - velB[2]; + vxab = velA[0] - velB[0]; + vyab = velA[1] - velB[1]; + vzab = velA[2] - velB[2]; - atoms[a]->getPos( posA ); - atoms[b]->getPos( posB ); + atoms[a]->getPos(posA); + atoms[b]->getPos(posB); - for (j = 0; j < 3; j++) + for (j = 0; j < 3; j++) rab[j] = posA[j] - posB[j]; - - info->wrapVector( rab ); - - rma = 1.0 / atoms[a]->getMass(); - rmb = 1.0 / atoms[b]->getMass(); - rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab; - - gab = -rvab / ( ( rma + rmb ) * constrainedDsqr[i] ); + info->wrapVector(rab); - if (fabs(gab) > tol) { - - dx = rab[0] * gab; - dy = rab[1] * gab; - dz = rab[2] * gab; - - velA[0] += rma * dx; - velA[1] += rma * dy; - velA[2] += rma * dz; + rma = 1.0 / atoms[a]->getMass(); + rmb = 1.0 / atoms[b]->getMass(); - atoms[a]->setVel( velA ); + rvab = rab[0] * vxab + rab[1] * vyab + rab[2] * vzab; - velB[0] -= rmb * dx; - velB[1] -= rmb * dy; - velB[2] -= rmb * dz; + gab = -rvab / ((rma + rmb) * constrainedDsqr[i]); - atoms[b]->setVel( velB ); - - moving[a] = 1; - moving[b] = 1; - done = 0; - } + if (fabs(gab) > tol){ + dx = rab[0] * gab; + dy = rab[1] * gab; + dz = rab[2] * gab; + + velA[0] += rma * dx; + velA[1] += rma * dy; + velA[2] += rma * dz; + + atoms[a]->setVel(velA); + + velB[0] -= rmb * dx; + velB[1] -= rmb * dy; + velB[2] -= rmb * dz; + + atoms[b]->setVel(velB); + + moving[a] = 1; + moving[b] = 1; + done = 0; + } } } - for(i=0; i void Integrator::rotate( int axes1, int axes2, double angle, double ji[3], - double A[3][3] ){ - - int i,j,k; +template void Integrator::rotate(int axes1, int axes2, + double angle, double ji[3], + double A[3][3]){ + int i, j, k; double sinAngle; double cosAngle; double angleSqr; @@ -707,16 +676,17 @@ template void Integrator::rotate( int a // initialize the tempA - for(i=0; i<3; i++){ - for(j=0; j<3; j++){ + for (i = 0; i < 3; i++){ + for (j = 0; j < 3; j++){ tempA[j][i] = A[i][j]; } } // initialize the tempJ - for( i=0; i<3; i++) tempJ[i] = ji[i]; - + for (i = 0; i < 3; i++) + tempJ[i] = ji[i]; + // initalize rot as a unit matrix rot[0][0] = 1.0; @@ -726,14 +696,14 @@ template void Integrator::rotate( int a rot[1][0] = 0.0; rot[1][1] = 1.0; rot[1][2] = 0.0; - + rot[2][0] = 0.0; rot[2][1] = 0.0; rot[2][2] = 1.0; - + // use a small angle aproximation for sin and cosine - angleSqr = angle * angle; + angleSqr = angle * angle; angleSqrOver4 = angleSqr / 4.0; top = 1.0 - angleSqrOver4; bottom = 1.0 + angleSqrOver4; @@ -746,12 +716,12 @@ template void Integrator::rotate( int a rot[axes1][axes2] = sinAngle; rot[axes2][axes1] = -sinAngle; - + // rotate the momentum acoording to: ji[] = rot[][] * ji[] - - for(i=0; i<3; i++){ + + for (i = 0; i < 3; i++){ ji[i] = 0.0; - for(k=0; k<3; k++){ + for (k = 0; k < 3; k++){ ji[i] += rot[i][k] * tempJ[k]; } } @@ -764,21 +734,20 @@ template void Integrator::rotate( int a // calculation as: // transpose(A[][]) = transpose(A[][]) * transpose(rot[][]) - for(i=0; i<3; i++){ - for(j=0; j<3; j++){ + for (i = 0; i < 3; i++){ + for (j = 0; j < 3; j++){ A[j][i] = 0.0; - for(k=0; k<3; k++){ - A[j][i] += tempA[i][k] * rot[j][k]; + for (k = 0; k < 3; k++){ + A[j][i] += tempA[i][k] * rot[j][k]; } } } } -template void Integrator::calcForce( int calcPot, int calcStress ){ - myFF->doForces(calcPot,calcStress); - +template void Integrator::calcForce(int calcPot, int calcStress){ + myFF->doForces(calcPot, calcStress); } template void Integrator::thermalize(){ - tStats->velocitize(); + tStats->velocitize(); }