6 |
|
* redistribute this software in source and binary code form, provided |
7 |
|
* that the following conditions are met: |
8 |
|
* |
9 |
< |
* 1. Acknowledgement of the program authors must be made in any |
10 |
< |
* publication of scientific results based in part on use of the |
11 |
< |
* program. An acceptable form of acknowledgement is citation of |
12 |
< |
* the article in which the program was described (Matthew |
13 |
< |
* A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher |
14 |
< |
* J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented |
15 |
< |
* Parallel Simulation Engine for Molecular Dynamics," |
16 |
< |
* J. Comput. Chem. 26, pp. 252-271 (2005)) |
17 |
< |
* |
18 |
< |
* 2. Redistributions of source code must retain the above copyright |
9 |
> |
* 1. Redistributions of source code must retain the above copyright |
10 |
|
* notice, this list of conditions and the following disclaimer. |
11 |
|
* |
12 |
< |
* 3. Redistributions in binary form must reproduce the above copyright |
12 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
* documentation and/or other materials provided with the |
15 |
|
* distribution. |
28 |
|
* arising out of the use of or inability to use software, even if the |
29 |
|
* University of Notre Dame has been advised of the possibility of |
30 |
|
* such damages. |
31 |
+ |
* |
32 |
+ |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
33 |
+ |
* research, please cite the appropriate papers when you publish your |
34 |
+ |
* work. Good starting points are: |
35 |
+ |
* |
36 |
+ |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
37 |
+ |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
38 |
+ |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
+ |
* [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). |
40 |
+ |
* [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). |
41 |
|
*/ |
42 |
|
|
43 |
|
#include <cmath> |
46 |
|
#include "io/StatWriter.hpp" |
47 |
|
#include "minimizers/Minimizer.hpp" |
48 |
|
#include "primitives/Molecule.hpp" |
49 |
< |
namespace oopse { |
50 |
< |
double dotProduct(const std::vector<double>& v1, const std::vector<double>& v2) { |
49 |
> |
#ifdef IS_MPI |
50 |
> |
#include <mpi.h> |
51 |
> |
#endif |
52 |
> |
namespace OpenMD { |
53 |
> |
RealType dotProduct(const std::vector<RealType>& v1, const std::vector<RealType>& v2) { |
54 |
|
if (v1.size() != v2.size()) { |
55 |
|
|
56 |
|
} |
57 |
|
|
58 |
|
|
59 |
< |
double result = 0.0; |
59 |
> |
RealType result = 0.0; |
60 |
|
for (unsigned int i = 0; i < v1.size(); ++i) { |
61 |
|
result += v1[i] * v2[i]; |
62 |
|
} |
68 |
|
info(rhs), usingShake(false) { |
69 |
|
|
70 |
|
forceMan = new ForceManager(info); |
71 |
< |
paramSet= new MinimizerParameterSet(info), |
68 |
< |
calcDim(); |
71 |
> |
paramSet= new MinimizerParameterSet(info), calcDim(); |
72 |
|
curX = getCoor(); |
73 |
|
curG.resize(ndim); |
74 |
|
|
79 |
|
delete paramSet; |
80 |
|
} |
81 |
|
|
82 |
< |
void Minimizer::calcEnergyGradient(std::vector<double> &x, |
83 |
< |
std::vector<double> &grad, double&energy, int&status) { |
82 |
> |
void Minimizer::calcEnergyGradient(std::vector<RealType> &x, |
83 |
> |
std::vector<RealType> &grad, RealType&energy, int&status) { |
84 |
|
|
85 |
|
SimInfo::MoleculeIterator i; |
86 |
|
Molecule::IntegrableObjectIterator j; |
87 |
|
Molecule* mol; |
88 |
|
StuntDouble* integrableObject; |
89 |
< |
std::vector<double> myGrad; |
89 |
> |
std::vector<RealType> myGrad; |
90 |
|
int shakeStatus; |
91 |
|
|
92 |
|
status = 1; |
121 |
|
|
122 |
|
} |
123 |
|
|
124 |
< |
void Minimizer::setCoor(std::vector<double> &x) { |
124 |
> |
void Minimizer::setCoor(std::vector<RealType> &x) { |
125 |
|
Vector3d position; |
126 |
|
Vector3d eulerAngle; |
127 |
|
SimInfo::MoleculeIterator i; |
152 |
|
|
153 |
|
} |
154 |
|
|
155 |
< |
std::vector<double> Minimizer::getCoor() { |
155 |
> |
std::vector<RealType> Minimizer::getCoor() { |
156 |
|
Vector3d position; |
157 |
|
Vector3d eulerAngle; |
158 |
|
SimInfo::MoleculeIterator i; |
160 |
|
Molecule* mol; |
161 |
|
StuntDouble* integrableObject; |
162 |
|
int index = 0; |
163 |
< |
std::vector<double> x(getDim()); |
163 |
> |
std::vector<RealType> x(getDim()); |
164 |
|
|
165 |
|
for (mol = info->beginMolecule(i); mol != NULL; mol = info->nextMolecule(i)) { |
166 |
|
for (integrableObject = mol->beginIntegrableObject(j); integrableObject != NULL; |
189 |
|
|
190 |
|
int done; |
191 |
|
|
192 |
< |
double posA[3], posB[3]; |
192 |
> |
RealType posA[3], posB[3]; |
193 |
|
|
194 |
< |
double velA[3], velB[3]; |
194 |
> |
RealType velA[3], velB[3]; |
195 |
|
|
196 |
< |
double pab[3]; |
196 |
> |
RealType pab[3]; |
197 |
|
|
198 |
< |
double rab[3]; |
198 |
> |
RealType rab[3]; |
199 |
|
|
200 |
|
int a, b, |
201 |
|
ax, ay, |
202 |
|
az, bx, |
203 |
|
by, bz; |
204 |
|
|
205 |
< |
double rma, rmb; |
205 |
> |
RealType rma, rmb; |
206 |
|
|
207 |
< |
double dx, dy, |
207 |
> |
RealType dx, dy, |
208 |
|
dz; |
209 |
|
|
210 |
< |
double rpab; |
210 |
> |
RealType rpab; |
211 |
|
|
212 |
< |
double rabsq, pabsq, |
212 |
> |
RealType rabsq, pabsq, |
213 |
|
rpabsq; |
214 |
|
|
215 |
< |
double diffsq; |
215 |
> |
RealType diffsq; |
216 |
|
|
217 |
< |
double gab; |
217 |
> |
RealType gab; |
218 |
|
|
219 |
|
int iteration; |
220 |
|
|
380 |
|
|
381 |
|
int done; |
382 |
|
|
383 |
< |
double posA[3], posB[3]; |
383 |
> |
RealType posA[3], posB[3]; |
384 |
|
|
385 |
< |
double frcA[3], frcB[3]; |
385 |
> |
RealType frcA[3], frcB[3]; |
386 |
|
|
387 |
< |
double rab[3], fpab[3]; |
387 |
> |
RealType rab[3], fpab[3]; |
388 |
|
|
389 |
|
int a, b, |
390 |
|
ax, ay, |
391 |
|
az, bx, |
392 |
|
by, bz; |
393 |
|
|
394 |
< |
double rma, rmb; |
394 |
> |
RealType rma, rmb; |
395 |
|
|
396 |
< |
double rvab; |
396 |
> |
RealType rvab; |
397 |
|
|
398 |
< |
double gab; |
398 |
> |
RealType gab; |
399 |
|
|
400 |
< |
double rabsq; |
400 |
> |
RealType rabsq; |
401 |
|
|
402 |
< |
double rfab; |
402 |
> |
RealType rfab; |
403 |
|
|
404 |
|
int iteration; |
405 |
|
|
527 |
|
calcEnergyGradient(curX, curG, curF, egEvalStatus); |
528 |
|
} |
529 |
|
|
530 |
< |
void Minimizer::calcF(std::vector < double > &x, double&f, int&status) { |
531 |
< |
std::vector < double > tempG; |
530 |
> |
void Minimizer::calcF(std::vector < RealType > &x, RealType&f, int&status) { |
531 |
> |
std::vector < RealType > tempG; |
532 |
|
|
533 |
|
tempG.resize(x.size()); |
534 |
|
|
541 |
|
calcEnergyGradient(curX, curG, curF, egEvalStatus); |
542 |
|
} |
543 |
|
|
544 |
< |
void Minimizer::calcG(std::vector<double>& x, std::vector<double>& g, double&f, int&status) { |
544 |
> |
void Minimizer::calcG(std::vector<RealType>& x, std::vector<RealType>& g, RealType&f, int&status) { |
545 |
|
calcEnergyGradient(x, g, f, status); |
546 |
|
} |
547 |
|
|
567 |
|
} |
568 |
|
} |
569 |
|
|
570 |
< |
void Minimizer::setX(std::vector < double > &x) { |
570 |
> |
void Minimizer::setX(std::vector < RealType > &x) { |
571 |
|
if (x.size() != ndim) { |
572 |
|
sprintf(painCave.errMsg, "Minimizer Error: dimesion of x and curX does not match\n"); |
573 |
|
painCave.isFatal = 1; |
577 |
|
curX = x; |
578 |
|
} |
579 |
|
|
580 |
< |
void Minimizer::setG(std::vector < double > &g) { |
580 |
> |
void Minimizer::setG(std::vector < RealType > &g) { |
581 |
|
if (g.size() != ndim) { |
582 |
|
sprintf(painCave.errMsg, "Minimizer Error: dimesion of g and curG does not match\n"); |
583 |
|
painCave.isFatal = 1; |
599 |
|
* @todo optimize this line search algorithm |
600 |
|
*/ |
601 |
|
|
602 |
< |
int Minimizer::doLineSearch(std::vector<double> &direction, |
603 |
< |
double stepSize) { |
602 |
> |
int Minimizer::doLineSearch(std::vector<RealType> &direction, |
603 |
> |
RealType stepSize) { |
604 |
|
|
605 |
< |
std::vector<double> xa; |
606 |
< |
std::vector<double> xb; |
607 |
< |
std::vector<double> xc; |
608 |
< |
std::vector<double> ga; |
609 |
< |
std::vector<double> gb; |
610 |
< |
std::vector<double> gc; |
611 |
< |
double fa; |
612 |
< |
double fb; |
613 |
< |
double fc; |
614 |
< |
double a; |
615 |
< |
double b; |
616 |
< |
double c; |
605 |
> |
std::vector<RealType> xa; |
606 |
> |
std::vector<RealType> xb; |
607 |
> |
std::vector<RealType> xc; |
608 |
> |
std::vector<RealType> ga; |
609 |
> |
std::vector<RealType> gb; |
610 |
> |
std::vector<RealType> gc; |
611 |
> |
RealType fa; |
612 |
> |
RealType fb; |
613 |
> |
RealType fc; |
614 |
> |
RealType a; |
615 |
> |
RealType b; |
616 |
> |
RealType c; |
617 |
|
int status; |
618 |
< |
double initSlope; |
619 |
< |
double slopeA; |
620 |
< |
double slopeB; |
621 |
< |
double slopeC; |
618 |
> |
RealType initSlope; |
619 |
> |
RealType slopeA; |
620 |
> |
RealType slopeB; |
621 |
> |
RealType slopeC; |
622 |
|
bool foundLower; |
623 |
|
int iter; |
624 |
|
int maxLSIter; |
625 |
< |
double mu; |
626 |
< |
double eta; |
627 |
< |
double ftol; |
628 |
< |
double lsTol; |
625 |
> |
RealType mu; |
626 |
> |
RealType eta; |
627 |
> |
RealType ftol; |
628 |
> |
RealType lsTol; |
629 |
|
|
630 |
|
xa.resize(ndim); |
631 |
|
xb.resize(ndim); |
833 |
|
int convgStatus; |
834 |
|
int stepStatus; |
835 |
|
int maxIter; |
836 |
< |
int writeFrq; |
836 |
> |
int writeFreq; |
837 |
|
int nextWriteIter; |
838 |
|
Snapshot* curSnapshot =info->getSnapshotManager()->getCurrentSnapshot(); |
839 |
|
DumpWriter dumpWriter(info); |
844 |
|
|
845 |
|
init(); |
846 |
|
|
847 |
< |
writeFrq = paramSet->getWriteFrq(); |
847 |
> |
writeFreq = paramSet->getWriteFreq(); |
848 |
|
|
849 |
< |
nextWriteIter = writeFrq; |
849 |
> |
nextWriteIter = writeFreq; |
850 |
|
|
851 |
|
maxIter = paramSet->getMaxIteration(); |
852 |
|
|
874 |
|
curSnapshot->increaseTime(1); |
875 |
|
|
876 |
|
if (curIter == nextWriteIter) { |
877 |
< |
nextWriteIter += writeFrq; |
877 |
> |
nextWriteIter += writeFreq; |
878 |
|
calcF(); |
879 |
< |
dumpWriter.writeDump(); |
879 |
> |
dumpWriter.writeDumpAndEor(); |
880 |
|
statWriter.writeStat(curSnapshot->statData); |
881 |
|
} |
882 |
|
|
905 |
|
} |
906 |
|
|
907 |
|
|
908 |
< |
double Minimizer::calcPotential() { |
909 |
< |
forceMan->calcForces(true, false); |
908 |
> |
RealType Minimizer::calcPotential() { |
909 |
> |
forceMan->calcForces(); |
910 |
|
|
911 |
|
Snapshot* curSnapshot = info->getSnapshotManager()->getCurrentSnapshot(); |
912 |
< |
double potential_local = curSnapshot->statData[Stats::LONG_RANGE_POTENTIAL] + |
912 |
> |
RealType potential_local = curSnapshot->statData[Stats::LONG_RANGE_POTENTIAL] + |
913 |
|
curSnapshot->statData[Stats::SHORT_RANGE_POTENTIAL] ; |
914 |
< |
double potential; |
914 |
> |
RealType potential; |
915 |
|
|
916 |
|
#ifdef IS_MPI |
917 |
< |
MPI_Allreduce(&potential_local, &potential, 1, MPI_DOUBLE, MPI_SUM, |
917 |
> |
MPI_Allreduce(&potential_local, &potential, 1, MPI_REALTYPE, MPI_SUM, |
918 |
|
MPI_COMM_WORLD); |
919 |
|
#else |
920 |
|
potential = potential_local; |