--- trunk/OOPSE-2.0/src/restraints/Restraints.cpp 2004/09/24 04:16:43 1490 +++ trunk/OOPSE-2.0/src/restraints/Restraints.cpp 2005/10/14 14:18:02 2365 @@ -1,397 +1,380 @@ -#include +/* + * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. + * + * The University of Notre Dame grants you ("Licensee") a + * non-exclusive, royalty free, license to use, modify and + * redistribute this software in source and binary code form, provided + * that the following conditions are met: + * + * 1. Acknowledgement of the program authors must be made in any + * publication of scientific results based in part on use of the + * program. An acceptable form of acknowledgement is citation of + * the article in which the program was described (Matthew + * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher + * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented + * Parallel Simulation Engine for Molecular Dynamics," + * J. Comput. Chem. 26, pp. 252-271 (2005)) + * + * 2. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 3. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * This software is provided "AS IS," without a warranty of any + * kind. All express or implied conditions, representations and + * warranties, including any implied warranty of merchantability, + * fitness for a particular purpose or non-infringement, are hereby + * excluded. The University of Notre Dame and its licensors shall not + * be liable for any damages suffered by licensee as a result of + * using, modifying or distributing the software or its + * derivatives. In no event will the University of Notre Dame or its + * licensors be liable for any lost revenue, profit or data, or for + * direct, indirect, special, consequential, incidental or punitive + * damages, however caused and regardless of the theory of liability, + * arising out of the use of or inability to use software, even if the + * University of Notre Dame has been advised of the possibility of + * such damages. + */ + #include -#include -#include -#include -#include -#include #include using namespace std; -#include "Restraints.hpp" -#include "SimInfo.hpp" -#include "simError.h" +#include "restraints/Restraints.hpp" +#include "primitives/Molecule.hpp" +#include "utils/simError.h" #define PI 3.14159265359 #define TWO_PI 6.28318530718 -Restraints::Restraints(double lambdaVal, double lambdaExp){ - lambdaValue = lambdaVal; - lambdaK = lambdaExp; - - const char *jolt = " \t\n;,"; - -#ifdef IS_MPI - if(worldRank == 0 ){ -#endif // is_mpi - - strcpy(springName, "HarmSpringConsts.txt"); - - ifstream springs(springName); - - if (!springs) { - sprintf(painCave.errMsg, - "In Restraints: Unable to open HarmSpringConsts.txt for reading.\n" - "\tDefault spring constants will be loaded. If you want to specify\n" - "\tspring constants, include a three line HarmSpringConsts.txt file\n" - "\tin the current directory.\n"); - painCave.severity = OOPSE_WARNING; - painCave.isFatal = 0; - simError(); - - // load default spring constants - kDist = 6; // spring constant in units of kcal/(mol*ang^2) - kTheta = 7.5; // in units of kcal/mol - kOmega = 13.5; // in units of kcal/mol - } else { - - springs.getline(inLine,999,'\n'); - springs.getline(inLine,999,'\n'); - token = strtok(inLine,jolt); - token = strtok(NULL,jolt); - strcpy(inValue,token); - kDist = (atof(inValue)); - springs.getline(inLine,999,'\n'); - token = strtok(inLine,jolt); - token = strtok(NULL,jolt); - strcpy(inValue,token); - kTheta = (atof(inValue)); - springs.getline(inLine,999,'\n'); - token = strtok(inLine,jolt); - token = strtok(NULL,jolt); - strcpy(inValue,token); - kOmega = (atof(inValue)); - springs.close(); - } -#ifdef IS_MPI - } +namespace oopse { - MPI_Bcast(&kDist, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); - MPI_Bcast(&kTheta, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); - MPI_Bcast(&kOmega, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); - - sprintf( checkPointMsg, - "Sucessfully opened and read spring file.\n"); - MPIcheckPoint(); + Restraints::Restraints(SimInfo* info, double lambdaVal, double lambdaExp){ + info_ = info; + Globals* simParam = info_->getSimParams(); -#endif // is_mpi - - sprintf(painCave.errMsg, - "The spring constants for thermodynamic integration are:\n" - "\tkDist = %lf\n" - "\tkTheta = %lf\n" - "\tkOmega = %lf\n", kDist, kTheta, kOmega); - painCave.severity = OOPSE_INFO; - painCave.isFatal = 0; - simError(); -} - -Restraints::~Restraints(){ -} - -void Restraints::Calc_rVal(double position[3], int currentMol){ - delRx = position[0] - cofmPosX[currentMol]; - delRy = position[1] - cofmPosY[currentMol]; - delRz = position[2] - cofmPosZ[currentMol]; - - return; -} - -void Restraints::Calc_body_thetaVal(double matrix[3][3], int currentMol){ - ub0x = matrix[0][0]*uX0[currentMol] + matrix[0][1]*uY0[currentMol] - + matrix[0][2]*uZ0[currentMol]; - ub0y = matrix[1][0]*uX0[currentMol] + matrix[1][1]*uY0[currentMol] - + matrix[1][2]*uZ0[currentMol]; - ub0z = matrix[2][0]*uX0[currentMol] + matrix[2][1]*uY0[currentMol] - + matrix[2][2]*uZ0[currentMol]; - - normalize = sqrt(ub0x*ub0x + ub0y*ub0y + ub0z*ub0z); - ub0x = ub0x/normalize; - ub0y = ub0y/normalize; - ub0z = ub0z/normalize; - - // Theta is the dot product of the reference and new z-axes - theta = acos(ub0z); - - return; -} - -void Restraints::Calc_body_omegaVal(double matrix[3][3], double zAngle){ - double zRotator[3][3]; - double tempOmega; - double wholeTwoPis; - // Use the omega accumulated from the rotation propagation - omega = zAngle; - - // translate the omega into a range between -PI and PI - if (omega < -PI){ - tempOmega = omega / -TWO_PI; - wholeTwoPis = floor(tempOmega); - tempOmega = omega + TWO_PI*wholeTwoPis; - if (tempOmega < -PI) - omega = tempOmega + TWO_PI; - else - omega = tempOmega; - } - if (omega > PI){ - tempOmega = omega / TWO_PI; - wholeTwoPis = floor(tempOmega); - tempOmega = omega - TWO_PI*wholeTwoPis; - if (tempOmega > PI) - omega = tempOmega - TWO_PI; - else - omega = tempOmega; - } - - vb0x = sin(omega); - vb0y = cos(omega); - vb0z = 0.0; - - normalize = sqrt(vb0x*vb0x + vb0y*vb0y + vb0z*vb0z); - vb0x = vb0x/normalize; - vb0y = vb0y/normalize; - vb0z = vb0z/normalize; - - return; -} - -double Restraints::Calc_Restraint_Forces(vector vecParticles){ - double pos[3]; - double A[3][3]; - double tolerance; - double tempPotent; - double factor; - double spaceTrq[3]; - double omegaPass; - - tolerance = 5.72957795131e-7; - - harmPotent = 0.0; // zero out the global harmonic potential variable - - factor = 1 - pow(lambdaValue, lambdaK); - - for (i=0; iisDirectional()){ - vecParticles[i]->getPos(pos); - vecParticles[i]->getA(A); - Calc_rVal( pos, i ); - Calc_body_thetaVal( A, i ); - omegaPass = vecParticles[i]->getZangle(); - Calc_body_omegaVal( A, omegaPass ); - - if (omega > PI || omega < -PI) - cout << "oops... " << omega << "\n"; - - // first we calculate the derivatives - dVdrx = -kDist*delRx; - dVdry = -kDist*delRy; - dVdrz = -kDist*delRz; - - // uTx... and vTx... are the body-fixed z and y unit vectors - uTx = 0.0; - uTy = 0.0; - uTz = 1.0; - vTx = 0.0; - vTy = 1.0; - vTz = 0.0; - - dVdux = 0; - dVduy = 0; - dVduz = 0; - dVdvx = 0; - dVdvy = 0; - dVdvz = 0; - - if (fabs(theta) > tolerance) { - dVdux = -(kTheta*theta/sin(theta))*ub0x; - dVduy = -(kTheta*theta/sin(theta))*ub0y; - dVduz = -(kTheta*theta/sin(theta))*ub0z; + lambdaValue = lambdaVal; + lambdaK = lambdaExp; + + if (simParam->getUseSolidThermInt()) { + if (simParam->haveThermIntDistSpringConst()) { + kDist = simParam->getThermIntDistSpringConst(); } - - if (fabs(omega) > tolerance) { - dVdvx = -(kOmega*omega/sin(omega))*vb0x; - dVdvy = -(kOmega*omega/sin(omega))*vb0y; - dVdvz = -(kOmega*omega/sin(omega))*vb0z; + else{ + kDist = simParam->getThermIntDistSpringConst(); + sprintf(painCave.errMsg, + "ThermoIntegration Warning: the spring constant for the\n" + "\ttranslational restraint was not specified. OOPSE will use\n" + "\ta default value of %f. To set it to something else, use\n" + "\tthe thermIntDistSpringConst variable.\n", + kDist); + painCave.isFatal = 0; + simError(); } - - // next we calculate the restraint forces and torques - restraintFrc[0] = dVdrx; - restraintFrc[1] = dVdry; - restraintFrc[2] = dVdrz; - tempPotent = 0.5*kDist*(delRx*delRx + delRy*delRy + delRz*delRz); - - restraintTrq[0] = 0.0; - restraintTrq[1] = 0.0; - restraintTrq[2] = 0.0; - - if (fabs(omega) > tolerance) { - restraintTrq[0] += 0.0; - restraintTrq[1] += 0.0; - restraintTrq[2] += vTy*dVdvx; - tempPotent += 0.5*(kOmega*omega*omega); + if (simParam->haveThermIntThetaSpringConst()) { + kTheta = simParam->getThermIntThetaSpringConst(); } - if (fabs(theta) > tolerance) { - restraintTrq[0] += (uTz*dVduy); - restraintTrq[1] += -(uTz*dVdux); - restraintTrq[2] += 0.0; - tempPotent += 0.5*(kTheta*theta*theta); + else{ + kTheta = simParam->getThermIntThetaSpringConst(); + sprintf(painCave.errMsg, + "ThermoIntegration Warning: the spring constant for the\n" + "\tdeflection orientational restraint was not specified.\n" + "\tOOPSE will use a default value of %f. To set it to\n" + "\tsomething else, use the thermIntThetaSpringConst variable.\n", + kTheta); + painCave.isFatal = 0; + simError(); } - - for (j = 0; j < 3; j++) { - restraintFrc[j] *= factor; - restraintTrq[j] *= factor; + if (simParam->haveThermIntOmegaSpringConst()) { + kOmega = simParam->getThermIntOmegaSpringConst(); } - - harmPotent += tempPotent; - - // now we need to convert from body-fixed torques to space-fixed torques - spaceTrq[0] = A[0][0]*restraintTrq[0] + A[1][0]*restraintTrq[1] - + A[2][0]*restraintTrq[2]; - spaceTrq[1] = A[0][1]*restraintTrq[0] + A[1][1]*restraintTrq[1] - + A[2][1]*restraintTrq[2]; - spaceTrq[2] = A[0][2]*restraintTrq[0] + A[1][2]*restraintTrq[1] - + A[2][2]*restraintTrq[2]; - - // now it's time to pass these temporary forces and torques - // to the total forces and torques - vecParticles[i]->addFrc(restraintFrc); - vecParticles[i]->addTrq(spaceTrq); + else{ + kOmega = simParam->getThermIntOmegaSpringConst(); + sprintf(painCave.errMsg, + "ThermoIntegration Warning: the spring constant for the\n" + "\tspin orientational restraint was not specified. OOPSE\n" + "\twill use a default value of %f. To set it to something\n" + "\telse, use the thermIntOmegaSpringConst variable.\n", + kOmega); + painCave.isFatal = 0; + simError(); + } } + + // build a RestReader and read in important information + + restRead_ = new RestReader(info_); + restRead_->readIdealCrystal(); + restRead_->readZangle(); + + delete restRead_; + restRead_ = NULL; + } - - // and we can return the appropriately scaled potential energy - tempPotent = harmPotent * factor; - return tempPotent; -} - -void Restraints::Store_Init_Info(vector vecParticles){ - double pos[3]; - double A[3][3]; - double RfromQ[3][3]; - double quat0, quat1, quat2, quat3; - double dot; -// char *token; -// char fileName[200]; -// char angleName[200]; -// char inLine[1000]; -// char inValue[200]; - const char *delimit = " \t\n;,"; - - //open the idealCrystal.in file and zAngle.ang file - strcpy(fileName, "idealCrystal.in"); - strcpy(angleName, "zAngle.ang"); - ifstream crystalIn(fileName); - ifstream angleIn(angleName); - - if (!crystalIn) { - sprintf(painCave.errMsg, - "Restraints Error: Unable to open idealCrystal.in for reading.\n" - "\tMake sure a reference crystal file is in the current directory.\n"); - painCave.isFatal = 1; - simError(); + Restraints::~Restraints(){ + } + + void Restraints::Calc_rVal(Vector3d &position, double refPosition[3]){ + delRx = position.x() - refPosition[0]; + delRy = position.y() - refPosition[1]; + delRz = position.z() - refPosition[2]; return; } - - if (!angleIn) { - sprintf(painCave.errMsg, - "Restraints Warning: The lack of a zAngle.ang file is mildly\n" - "\tunsettling... This means the simulation is starting from the\n" - "\tidealCrystal.in reference configuration, so the omega values\n" - "\twill all be set to zero. If this is not the case, you should\n" - "\tquestion your results.\n"); - painCave.isFatal = 0; - simError(); + + void Restraints::Calc_body_thetaVal(RotMat3x3d &matrix, double refUnit[3]){ + ub0x = matrix(0,0)*refUnit[0] + matrix(0,1)*refUnit[1] + + matrix(0,2)*refUnit[2]; + ub0y = matrix(1,0)*refUnit[0] + matrix(1,1)*refUnit[1] + + matrix(1,2)*refUnit[2]; + ub0z = matrix(2,0)*refUnit[0] + matrix(2,1)*refUnit[1] + + matrix(2,2)*refUnit[2]; + + normalize = sqrt(ub0x*ub0x + ub0y*ub0y + ub0z*ub0z); + ub0x = ub0x/normalize; + ub0y = ub0y/normalize; + ub0z = ub0z/normalize; + + // Theta is the dot product of the reference and new z-axes + theta = acos(ub0z); + + return; } - - // A rather specific reader for OOPSE .eor files... - // Let's read in the perfect crystal file - crystalIn.getline(inLine,999,'\n'); - crystalIn.getline(inLine,999,'\n'); - for (i=0; i PI){ + tempOmega = omega / TWO_PI; + wholeTwoPis = floor(tempOmega); + tempOmega = omega - TWO_PI*wholeTwoPis; + if (tempOmega > PI) + omega = tempOmega - TWO_PI; + else + omega = tempOmega; + } + + vb0x = sin(omega); + vb0y = cos(omega); + vb0z = 0.0; + + normalize = sqrt(vb0x*vb0x + vb0y*vb0y + vb0z*vb0z); + vb0x = vb0x/normalize; + vb0y = vb0y/normalize; + vb0z = vb0z/normalize; + + return; } - - // now we can read in the zAngle.ang file - if (angleIn){ - angleIn.getline(inLine,999,'\n'); - for (i=0; isetZangle(atof(inValue)); + + double Restraints::Calc_Restraint_Forces(){ + SimInfo::MoleculeIterator mi; + Molecule* mol; + Molecule::IntegrableObjectIterator ii; + StuntDouble* integrableObject; + Vector3d pos; + RotMat3x3d A; + double refPos[3]; + double refVec[3]; + double tolerance; + double tempPotent; + double factor; + double spaceTrq[3]; + double omegaPass; + GenericData* data; + DoubleGenericData* doubleData; + + tolerance = 5.72957795131e-7; + + harmPotent = 0.0; // zero out the global harmonic potential variable + + factor = 1 - pow(lambdaValue, lambdaK); + + for (mol = info_->beginMolecule(mi); mol != NULL; + mol = info_->nextMolecule(mi)) { + for (integrableObject = mol->beginIntegrableObject(ii); + integrableObject != NULL; + integrableObject = mol->nextIntegrableObject(ii)) { + + // obtain the current and reference positions + pos = integrableObject->getPos(); + + data = integrableObject->getPropertyByName("refPosX"); + if (data){ + doubleData = dynamic_cast(data); + if (!doubleData){ + cerr << "Can't obtain refPosX from StuntDouble\n"; + return 0.0; + } + else refPos[0] = doubleData->getData(); + } + data = integrableObject->getPropertyByName("refPosY"); + if (data){ + doubleData = dynamic_cast(data); + if (!doubleData){ + cerr << "Can't obtain refPosY from StuntDouble\n"; + return 0.0; + } + else refPos[1] = doubleData->getData(); + } + data = integrableObject->getPropertyByName("refPosZ"); + if (data){ + doubleData = dynamic_cast(data); + if (!doubleData){ + cerr << "Can't obtain refPosZ from StuntDouble\n"; + return 0.0; + } + else refPos[2] = doubleData->getData(); + } + + // calculate the displacement + Calc_rVal( pos, refPos ); + + // calculate the derivatives + dVdrx = -kDist*delRx; + dVdry = -kDist*delRy; + dVdrz = -kDist*delRz; + + // next we calculate the restraint forces + restraintFrc[0] = dVdrx; + restraintFrc[1] = dVdry; + restraintFrc[2] = dVdrz; + tempPotent = 0.5*kDist*(delRx*delRx + delRy*delRy + delRz*delRz); + + // apply the lambda scaling factor to the forces + for (j = 0; j < 3; j++) restraintFrc[j] *= factor; + + // and add the temporary force to the total force + integrableObject->addFrc(restraintFrc); + + // if the particle is directional, we accumulate the rot. restraints + if (integrableObject->isDirectional()){ + + // get the current rotation matrix and reference vector + A = integrableObject->getA(); + + data = integrableObject->getPropertyByName("refVectorX"); + if (data){ + doubleData = dynamic_cast(data); + if (!doubleData){ + cerr << "Can't obtain refVectorX from StuntDouble\n"; + return 0.0; + } + else refVec[0] = doubleData->getData(); + } + data = integrableObject->getPropertyByName("refVectorY"); + if (data){ + doubleData = dynamic_cast(data); + if (!doubleData){ + cerr << "Can't obtain refVectorY from StuntDouble\n"; + return 0.0; + } + else refVec[1] = doubleData->getData(); + } + data = integrableObject->getPropertyByName("refVectorZ"); + if (data){ + doubleData = dynamic_cast(data); + if (!doubleData){ + cerr << "Can't obtain refVectorZ from StuntDouble\n"; + return 0.0; + } + else refVec[2] = doubleData->getData(); + } + + // calculate the theta and omega displacements + Calc_body_thetaVal( A, refVec ); + omegaPass = integrableObject->getZangle(); + Calc_body_omegaVal( omegaPass ); + + // uTx... and vTx... are the body-fixed z and y unit vectors + uTx = 0.0; + uTy = 0.0; + uTz = 1.0; + vTx = 0.0; + vTy = 1.0; + vTz = 0.0; + + dVdux = 0.0; + dVduy = 0.0; + dVduz = 0.0; + dVdvx = 0.0; + dVdvy = 0.0; + dVdvz = 0.0; + + if (fabs(theta) > tolerance) { + dVdux = -(kTheta*theta/sin(theta))*ub0x; + dVduy = -(kTheta*theta/sin(theta))*ub0y; + dVduz = -(kTheta*theta/sin(theta))*ub0z; + } + + if (fabs(omega) > tolerance) { + dVdvx = -(kOmega*omega/sin(omega))*vb0x; + dVdvy = -(kOmega*omega/sin(omega))*vb0y; + dVdvz = -(kOmega*omega/sin(omega))*vb0z; + } + + // next we calculate the restraint torques + restraintTrq[0] = 0.0; + restraintTrq[1] = 0.0; + restraintTrq[2] = 0.0; + + if (fabs(omega) > tolerance) { + restraintTrq[0] += 0.0; + restraintTrq[1] += 0.0; + restraintTrq[2] += vTy*dVdvx; + tempPotent += 0.5*(kOmega*omega*omega); + } + if (fabs(theta) > tolerance) { + restraintTrq[0] += (uTz*dVduy); + restraintTrq[1] += -(uTz*dVdux); + restraintTrq[2] += 0.0; + tempPotent += 0.5*(kTheta*theta*theta); + } + + // apply the lambda scaling factor to these torques + for (j = 0; j < 3; j++) restraintTrq[j] *= factor; + + // now we need to convert from body-fixed to space-fixed torques + spaceTrq[0] = A(0,0)*restraintTrq[0] + A(1,0)*restraintTrq[1] + + A(2,0)*restraintTrq[2]; + spaceTrq[1] = A(0,1)*restraintTrq[0] + A(1,1)*restraintTrq[1] + + A(2,1)*restraintTrq[2]; + spaceTrq[2] = A(0,2)*restraintTrq[0] + A(1,2)*restraintTrq[1] + + A(2,2)*restraintTrq[2]; + + // now pass this temporary torque vector to the total torque + integrableObject->addTrq(spaceTrq); + } + + // update the total harmonic potential with this object's contribution + harmPotent += tempPotent; + } + } + + // we can finish by returning the appropriately scaled potential energy + tempPotent = harmPotent * factor; + + return tempPotent; + } - - return; -} - -void Restraints::Write_zAngle_File(vector vecParticles){ - - char zOutName[200]; - - strcpy(zOutName,"zAngle.ang"); - - ofstream angleOut(zOutName); - angleOut << "This file contains the omega values for the .eor file\n"; - for (i=0; igetZangle() << "\n"; - } - return; -} - -double Restraints::getVharm(){ - return harmPotent; -} - + +}// end namespace oopse