2 |
|
#include "simError.h" |
3 |
|
#include <cmath> |
4 |
|
template<typename T> ZConstraint<T>::ZConstraint(SimInfo* theInfo, ForceFields* the_ff) |
5 |
< |
: T(theInfo, the_ff), fz(NULL), curZPos(NULL), |
5 |
> |
: T(theInfo, the_ff), fz(NULL), curZPos(NULL), fzOut(NULL), |
6 |
|
indexOfZConsMols(NULL), forcePolicy(NULL), curZconsTime(0) |
7 |
|
{ |
8 |
|
|
27 |
|
double halfOfLargestBox = max(info->boxL[0], max(info->boxL[1], info->boxL[2])) /2; |
28 |
|
zForceConst = Kb * info->target_temp /(halfOfLargestBox * halfOfLargestBox); |
29 |
|
|
30 |
< |
//creat force substraction policy |
30 |
> |
//creat force Subtraction policy |
31 |
|
data = info->getProperty(ZCONSFORCEPOLICY_ID); |
32 |
|
if(!data){ |
33 |
|
sprintf( painCave.errMsg, |
34 |
< |
"ZConstraint Warning: User does not set force substraction policy, " |
35 |
< |
"average force substraction policy is used\n"); |
34 |
> |
"ZConstraint Warning: User does not set force Subtraction policy, " |
35 |
> |
"PolicyByMass is used\n"); |
36 |
|
painCave.isFatal = 0; |
37 |
|
simError(); |
38 |
|
|
39 |
< |
forcePolicy = (ForceSubstractionPolicy*) new PolicyByNumber(this); |
39 |
> |
forcePolicy = (ForceSubtractionPolicy*) new PolicyByMass(this); |
40 |
|
} |
41 |
|
else{ |
42 |
|
policy = dynamic_cast<StringData*>(data); |
44 |
|
if(!policy){ |
45 |
|
sprintf( painCave.errMsg, |
46 |
|
"ZConstraint Error: Convertion from GenericData to StringData failure, " |
47 |
< |
"average force substraction policy is used\n"); |
47 |
> |
"PolicyByMass is used\n"); |
48 |
|
painCave.isFatal = 0; |
49 |
|
simError(); |
50 |
|
|
51 |
< |
forcePolicy = (ForceSubstractionPolicy*) new PolicyByNumber(this); |
51 |
> |
forcePolicy = (ForceSubtractionPolicy*) new PolicyByMass(this); |
52 |
|
} |
53 |
|
else{ |
54 |
|
if(policy->getData() == "BYNUMBER") |
55 |
< |
forcePolicy = (ForceSubstractionPolicy*) new PolicyByNumber(this); |
55 |
> |
forcePolicy = (ForceSubtractionPolicy*) new PolicyByNumber(this); |
56 |
|
else if(policy->getData() == "BYMASS") |
57 |
< |
forcePolicy = (ForceSubstractionPolicy*) new PolicyByMass(this); |
57 |
> |
forcePolicy = (ForceSubtractionPolicy*) new PolicyByMass(this); |
58 |
|
else{ |
59 |
|
sprintf( painCave.errMsg, |
60 |
< |
"ZConstraint Warning: unknown force substraction policy, " |
61 |
< |
"average force substraction policy is used\n"); |
60 |
> |
"ZConstraint Warning: unknown force Subtraction policy, " |
61 |
> |
"PolicyByMass is used\n"); |
62 |
|
painCave.isFatal = 0; |
63 |
|
simError(); |
64 |
+ |
forcePolicy = (ForceSubtractionPolicy*) new PolicyByMass(this); |
65 |
|
} |
66 |
|
} |
67 |
|
} |
250 |
|
(*parameters)[i].zPos = COM[whichDirection]; |
251 |
|
|
252 |
|
sprintf( painCave.errMsg, |
253 |
< |
"ZConstraint warningr: Does not specify zpos for z-constraint molecule " |
253 |
> |
"ZConstraint warning: Does not specify zpos for z-constraint molecule " |
254 |
|
"initial z coornidate will be used \n"); |
255 |
|
painCave.isFatal = 0; |
256 |
|
simError(); |
277 |
|
massOfZConsMols.push_back(molecules[i].getTotalMass()); |
278 |
|
|
279 |
|
zPos.push_back((*parameters)[searchResult].zPos); |
280 |
< |
cout << "index: "<< (*parameters)[searchResult].zconsIndex |
281 |
< |
<<"\tzPos = " << (*parameters)[searchResult].zPos << endl; |
280 |
> |
// cout << "index: "<< (*parameters)[searchResult].zconsIndex |
281 |
> |
// <<"\tzPos = " << (*parameters)[searchResult].zPos << endl; |
282 |
|
kz.push_back((*parameters)[searchResult]. kRatio * zForceConst); |
283 |
|
|
284 |
|
molecules[i].getCOM(COM); |
330 |
|
MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); |
331 |
|
#endif |
332 |
|
|
332 |
– |
|
333 |
|
//get total number of unconstrained atoms |
334 |
|
int nUnconsAtoms_local; |
335 |
|
nUnconsAtoms_local = 0; |
343 |
|
MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
344 |
|
#endif |
345 |
|
|
346 |
– |
// creat zconsWriter |
347 |
– |
fzOut = new ZConsWriter(zconsOutput.c_str(), parameters); |
348 |
– |
|
349 |
– |
if(!fzOut){ |
350 |
– |
sprintf( painCave.errMsg, |
351 |
– |
"Memory allocation failure in class Zconstraint\n"); |
352 |
– |
painCave.isFatal = 1; |
353 |
– |
simError(); |
354 |
– |
} |
355 |
– |
|
346 |
|
forcePolicy->update(); |
347 |
|
} |
348 |
|
|
495 |
|
} |
496 |
|
|
497 |
|
template<typename T> void ZConstraint<T>::integrate(){ |
498 |
+ |
|
499 |
+ |
// creat zconsWriter |
500 |
+ |
fzOut = new ZConsWriter(zconsOutput.c_str(), parameters); |
501 |
|
|
502 |
+ |
if(!fzOut){ |
503 |
+ |
sprintf( painCave.errMsg, |
504 |
+ |
"Memory allocation failure in class Zconstraint\n"); |
505 |
+ |
painCave.isFatal = 1; |
506 |
+ |
simError(); |
507 |
+ |
} |
508 |
+ |
|
509 |
|
//zero out the velocities of center of mass of unconstrained molecules |
510 |
|
//and the velocities of center of mass of every single z-constrained molecueles |
511 |
|
zeroOutVel(); |
512 |
+ |
|
513 |
+ |
curZconsTime = zconsTime + info->getTime(); |
514 |
|
|
515 |
|
T::integrate(); |
516 |
|
|
527 |
|
double zsys; |
528 |
|
double COM[3]; |
529 |
|
double force[3]; |
530 |
+ |
double zSysCOMVel; |
531 |
|
|
532 |
|
T::calcForce(calcPot, calcStress); |
533 |
|
|
534 |
< |
if (checkZConsState()){ |
535 |
< |
zeroOutVel(); |
536 |
< |
forcePolicy->update(); |
534 |
> |
if (checkZConsState()){ |
535 |
> |
zeroOutVel(); |
536 |
> |
forcePolicy->update(); |
537 |
|
} |
538 |
+ |
|
539 |
|
zsys = calcZSys(); |
540 |
< |
cout << "---------------------------------------------------------------------" <<endl; |
541 |
< |
cout << "current time: " << info->getTime() << endl; |
542 |
< |
cout << "center of mass at z: " << zsys << endl; |
543 |
< |
//cout << "before calcForce, the COMVel of moving molecules is " << calcMovingMolsCOMVel() <<endl; |
544 |
< |
cout << "before calcForce, the COMVel of system is " << calcSysCOMVel() <<endl; |
540 |
> |
zSysCOMVel = calcSysCOMVel(); |
541 |
> |
#ifdef IS_MPI |
542 |
> |
if(worldRank == 0){ |
543 |
> |
#endif |
544 |
> |
//cout << "---------------------------------------------------------------------" <<endl; |
545 |
> |
//cout << "current time: " << info->getTime() << endl; |
546 |
> |
//cout << "center of mass at z: " << zsys << endl; |
547 |
> |
//cout << "before calcForce, the COMVel of system is " << zSysCOMVel <<endl; |
548 |
|
|
549 |
< |
//cout << "before doZConstraintForce, totalForce is " << calcTotalForce() << endl; |
549 |
> |
#ifdef IS_MPI |
550 |
> |
} |
551 |
> |
#endif |
552 |
|
|
553 |
|
//do zconstraint force; |
554 |
|
if (haveFixedZMols()) |
555 |
|
this->doZconstraintForce(); |
556 |
< |
|
556 |
> |
|
557 |
|
//use harmonical poteintial to move the molecules to the specified positions |
558 |
|
if (haveMovingZMols()) |
559 |
|
this->doHarmonic(); |
560 |
|
|
552 |
– |
//cout << "after doHarmonic, totalForce is " << calcTotalForce() << endl; |
553 |
– |
|
561 |
|
//write out forces and current positions of z-constraint molecules |
562 |
|
if(info->getTime() >= curZconsTime){ |
563 |
|
for(int i = 0; i < zconsMols.size(); i++){ |
578 |
|
fzOut->writeFZ(info->getTime(), zconsMols.size(), indexOfZConsMols, fz, curZPos); |
579 |
|
curZconsTime += zconsTime; |
580 |
|
} |
581 |
< |
|
582 |
< |
//cout << "after calcForce, the COMVel of moving molecules is " << calcMovingMolsCOMVel() <<endl; |
583 |
< |
cout << "after calcForce, the COMVel of system is " << calcSysCOMVel() <<endl; |
581 |
> |
|
582 |
> |
zSysCOMVel = calcSysCOMVel(); |
583 |
> |
#ifdef IS_MPI |
584 |
> |
if(worldRank == 0){ |
585 |
> |
#endif |
586 |
> |
//cout << "after calcForce, the COMVel of system is " << zSysCOMVel <<endl; |
587 |
> |
#ifdef IS_MPI |
588 |
> |
} |
589 |
> |
#endif |
590 |
|
} |
591 |
|
|
592 |
|
|
660 |
|
if (states[i] == zcsFixed){ |
661 |
|
|
662 |
|
zconsMols[i]->getCOMvel(COMvel); |
663 |
< |
//cout << "before resetting " << indexOfZConsMols[i] <<"'s vz is " << COMvel[whichDirection] << endl; |
663 |
> |
//cout << "before resetting " << indexOfZConsMols[i] <<"'s vz is " << COMvel[whichDirection] << endl; |
664 |
|
|
665 |
|
fixedZAtoms = zconsMols[i]->getMyAtoms(); |
666 |
|
|
682 |
|
#ifdef IS_MPI |
683 |
|
if(worldRank == 0){ |
684 |
|
#endif |
685 |
< |
cout << "before resetting the COMVel of sytem is " << zSysCOMVel << endl; |
685 |
> |
//cout << "before resetting the COMVel of sytem is " << zSysCOMVel << endl; |
686 |
|
#ifdef IS_MPI |
687 |
|
} |
688 |
|
#endif |
756 |
|
#ifdef IS_MPI |
757 |
|
if(worldRank == 0){ |
758 |
|
#endif |
759 |
< |
cout << "after resetting the COMVel of moving molecules is " << zSysCOMVel << endl; |
759 |
> |
//cout << "after resetting the COMVel of moving molecules is " << zSysCOMVel << endl; |
760 |
|
#ifdef IS_MPI |
761 |
|
} |
762 |
|
#endif |
776 |
|
double COM[3]; |
777 |
|
double force[3]; |
778 |
|
|
766 |
– |
|
767 |
– |
|
779 |
|
//constrain the molecules which do not reach the specified positions |
780 |
|
|
781 |
|
//Zero Out the force of z-contrained molecules |
782 |
|
totalFZ_local = 0; |
783 |
|
|
784 |
|
//calculate the total z-contrained force of fixed z-contrained molecules |
785 |
+ |
|
786 |
+ |
//cout << "before zero out z-constraint force on fixed z-constraint molecuels " |
787 |
+ |
// << "total force is " << calcTotalForce() << endl; |
788 |
|
|
789 |
|
for(int i = 0; i < zconsMols.size(); i++){ |
790 |
|
|
800 |
|
} |
801 |
|
totalFZ_local += fz[i]; |
802 |
|
|
803 |
< |
cout << "Fixed Molecule --\tindex: " << indexOfZConsMols[i] |
804 |
< |
<<"\tcurrent zpos: " << COM[whichDirection] |
805 |
< |
<< "\tcurrent fz: " <<fz[i] << endl; |
803 |
> |
//cout << "Fixed Molecule\tindex: " << indexOfZConsMols[i] |
804 |
> |
// <<"\tcurrent zpos: " << COM[whichDirection] |
805 |
> |
// << "\tcurrent fz: " <<fz[i] << endl; |
806 |
|
|
807 |
+ |
|
808 |
|
} |
809 |
|
|
810 |
|
} |
830 |
|
zconsAtoms = zconsMols[i]->getMyAtoms(); |
831 |
|
|
832 |
|
for(int j =0; j < nAtomOfCurZConsMol; j++) { |
833 |
< |
force[whichDirection] = -fz[i]/ nAtomOfCurZConsMol; |
834 |
< |
//force[whichDirection] = - forcePolicy->getZFOfFixedZMols(zconsMols[i], zconsAtoms[j], fz[i]); |
833 |
> |
//force[whichDirection] = -fz[i]/ nAtomOfCurZConsMol; |
834 |
> |
force[whichDirection] = - forcePolicy->getZFOfFixedZMols(zconsMols[i], zconsAtoms[j], fz[i]); |
835 |
|
zconsAtoms[j]->addFrc(force); |
836 |
|
} |
837 |
|
|
840 |
|
} |
841 |
|
|
842 |
|
//cout << "after zero out z-constraint force on fixed z-constraint molecuels " |
843 |
< |
// << "total force is " << calcTotalForce() << endl; |
843 |
> |
// << "total force is " << calcTotalForce() << endl; |
844 |
> |
|
845 |
|
|
830 |
– |
//calculate the number of atoms of moving z-constrained molecules |
831 |
– |
int nMovingZAtoms_local; |
832 |
– |
int nMovingZAtoms; |
833 |
– |
|
834 |
– |
nMovingZAtoms_local = 0; |
835 |
– |
for(int i = 0; i < zconsMols.size(); i++) |
836 |
– |
if(states[i] == zcsMoving) |
837 |
– |
nMovingZAtoms_local += zconsMols[i]->getNAtoms(); |
838 |
– |
|
839 |
– |
#ifdef IS_MPI |
840 |
– |
MPI_Allreduce(&nMovingZAtoms_local, &nMovingZAtoms, 1, |
841 |
– |
MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); |
842 |
– |
#else |
843 |
– |
nMovingZAtoms = nMovingZAtoms_local; |
844 |
– |
#endif |
845 |
– |
|
846 |
|
force[0]= 0; |
847 |
|
force[1]= 0; |
848 |
|
force[2]= 0; |
853 |
|
Atom** unconsAtoms = unconsMols[i]->getMyAtoms(); |
854 |
|
|
855 |
|
for(int j = 0; j < unconsMols[i]->getNAtoms(); j++){ |
856 |
< |
force[whichDirection] = totalFZ / (totNumOfUnconsAtoms + nMovingZAtoms); |
857 |
< |
//force[whichDirection] = forcePolicy->getZFOfMovingMols(unconsAtoms[j],totalFZ); |
856 |
> |
//force[whichDirection] = totalFZ / (totNumOfUnconsAtoms + nMovingZAtoms); |
857 |
> |
force[whichDirection] = forcePolicy->getZFOfMovingMols(unconsAtoms[j],totalFZ); |
858 |
|
unconsAtoms[j]->addFrc(force); |
859 |
|
} |
860 |
|
|
867 |
|
Atom** movingZAtoms = zconsMols[i]->getMyAtoms(); |
868 |
|
|
869 |
|
for(int j = 0; j < zconsMols[i]->getNAtoms(); j++){ |
870 |
< |
force[whichDirection] = totalFZ / (totNumOfUnconsAtoms + nMovingZAtoms); |
871 |
< |
//force[whichDirection] = forcePolicy->getZFOfMovingMols(movingZAtoms[j],totalFZ); |
870 |
> |
//force[whichDirection] = totalFZ / (totNumOfUnconsAtoms + nMovingZAtoms); |
871 |
> |
force[whichDirection] = forcePolicy->getZFOfMovingMols(movingZAtoms[j],totalFZ); |
872 |
|
movingZAtoms[j]->addFrc(force); |
873 |
|
} |
874 |
|
} |
875 |
|
} |
876 |
+ |
// cout << "after substracting z-constraint force from moving molecuels " |
877 |
+ |
// << "total force is " << calcTotalForce() << endl; |
878 |
|
|
877 |
– |
//cout << "after substracting z-constraint force from moving molecuels " |
878 |
– |
// << "total force is " << calcTotalForce() << endl; |
879 |
– |
|
879 |
|
} |
880 |
|
|
881 |
|
/** |
902 |
|
|
903 |
|
if (states[i] == zcsMoving){ |
904 |
|
zconsMols[i]->getCOM(COM); |
905 |
< |
cout << "Moving Molecule --\tindex: " << indexOfZConsMols[i] <<"\tcurrent zpos: " << COM[whichDirection] << endl; |
905 |
> |
// cout << "Moving Molecule\tindex: " << indexOfZConsMols[i] |
906 |
> |
// << "\tcurrent zpos: " << COM[whichDirection] << endl; |
907 |
> |
|
908 |
> |
diff = COM[whichDirection] -zPos[i]; |
909 |
|
|
908 |
– |
diff = COM[whichDirection] -zPos[i]; |
909 |
– |
|
910 |
|
harmonicU = 0.5 * kz[i] * diff * diff; |
911 |
< |
info->lrPot += harmonicU; |
911 |
> |
info->lrPot += harmonicU; |
912 |
|
|
913 |
|
harmonicF = - kz[i] * diff; |
914 |
|
totalFZ_local += harmonicF; |
918 |
|
Atom** movingZAtoms = zconsMols[i]->getMyAtoms(); |
919 |
|
|
920 |
|
for(int j = 0; j < zconsMols[i]->getNAtoms(); j++){ |
921 |
< |
force[whichDirection] = harmonicF / zconsMols[i]->getNAtoms(); |
922 |
< |
//force[whichDirection] = forcePolicy->getHFOfFixedZMols(zconsMols[i], movingZAtoms[j], harmonicF); |
921 |
> |
//force[whichDirection] = harmonicF / zconsMols[i]->getNAtoms(); |
922 |
> |
force[whichDirection] = forcePolicy->getHFOfFixedZMols(zconsMols[i], movingZAtoms[j], harmonicF); |
923 |
|
movingZAtoms[j]->addFrc(force); |
924 |
|
} |
925 |
|
} |
932 |
|
MPI_Allreduce(&totalFZ_local, &totalFZ, 1, MPI_DOUBLE,MPI_SUM, MPI_COMM_WORLD); |
933 |
|
#endif |
934 |
|
|
935 |
+ |
cout << "before substracting harmonic force from moving molecuels " |
936 |
+ |
<< "total force is " << calcTotalForce() << endl; |
937 |
+ |
|
938 |
|
force[0]= 0; |
939 |
|
force[1]= 0; |
940 |
|
force[2]= 0; |
945 |
|
Atom** unconsAtoms = unconsMols[i]->getMyAtoms(); |
946 |
|
|
947 |
|
for(int j = 0; j < unconsMols[i]->getNAtoms(); j++){ |
948 |
< |
force[whichDirection] = - totalFZ /totNumOfUnconsAtoms; |
949 |
< |
//force[whichDirection] = - forcePolicy->getHFOfUnconsMols(unconsAtoms[j], totalFZ); |
948 |
> |
//force[whichDirection] = - totalFZ /totNumOfUnconsAtoms; |
949 |
> |
force[whichDirection] = - forcePolicy->getHFOfUnconsMols(unconsAtoms[j], totalFZ); |
950 |
|
unconsAtoms[j]->addFrc(force); |
951 |
|
} |
952 |
|
} |
953 |
|
|
954 |
+ |
cout << "after substracting harmonic force from moving molecuels " |
955 |
+ |
<< "total force is " << calcTotalForce() << endl; |
956 |
+ |
|
957 |
|
} |
958 |
|
|
959 |
|
/** |
990 |
|
MPI_Allreduce(&changed_local, &changed, 1, MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
991 |
|
#endif |
992 |
|
|
993 |
< |
return changed > 0 ? true : false; |
993 |
> |
return (changed > 0); |
994 |
> |
|
995 |
|
} |
996 |
|
|
997 |
|
template<typename T> bool ZConstraint<T>::haveFixedZMols(){ |
1013 |
|
MPI_Allreduce(&havingFixed_local, &havingFixed, 1, MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
1014 |
|
#endif |
1015 |
|
|
1016 |
< |
return havingFixed > 0 ? true : false; |
1016 |
> |
return (havingFixed > 0); |
1017 |
|
} |
1018 |
|
|
1019 |
|
|
1039 |
|
MPI_Allreduce(&havingMoving_local, &havingMoving, 1, MPI_INT,MPI_SUM, MPI_COMM_WORLD); |
1040 |
|
#endif |
1041 |
|
|
1042 |
< |
return havingMoving > 0 ? true : false; |
1042 |
> |
return (havingMoving > 0); |
1043 |
|
|
1044 |
|
} |
1045 |
|
|
1207 |
|
#else |
1208 |
|
massOfMovingZAtoms = massOfMovingZAtoms_local; |
1209 |
|
#endif |
1210 |
< |
totMassOfMovingAtoms = massOfMovingZAtoms_local + zconsIntegrator->totalMassOfUncons; |
1210 |
> |
totMassOfMovingAtoms = massOfMovingZAtoms + zconsIntegrator->totalMassOfUncons; |
1211 |
|
} |
1212 |
|
|
1213 |
|
template<typename T> double ZConstraint<T>::PolicyByMass::getZFOfFixedZMols(Molecule* mol, Atom* atom, double totalForce){ |