--- branches/development/src/flucq/FluctuatingChargePropagator.cpp 2012/05/31 12:25:30 1731 +++ trunk/src/flucq/FluctuatingChargePropagator.cpp 2014/04/14 18:32:51 1981 @@ -35,73 +35,92 @@ * * [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). * [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). - * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). + * [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). * [4] Kuang & Gezelter, J. Chem. Phys. 133, 164101 (2010). * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). */ -#include "FluctuatingChargePropagator.hpp" -#include "primitives/Molecule.hpp" -#include "utils/simError.h" -#include "utils/PhysicalConstants.hpp" +#include "flucq/FluctuatingChargePropagator.hpp" +#include "flucq/FluctuatingChargeObjectiveFunction.hpp" +#include "optimization/Constraint.hpp" +#include "optimization/Problem.hpp" +#include "optimization/EndCriteria.hpp" +#include "optimization/StatusFunction.hpp" +#include "optimization/OptimizationFactory.hpp" + #ifdef IS_MPI #include #endif +using namespace QuantLib; namespace OpenMD { - void FluctuatingChargePropagator::applyConstraints() { - if (!hasFlucQ_) return; + FluctuatingChargePropagator::FluctuatingChargePropagator(SimInfo* info) : + info_(info), hasFlucQ_(false), forceMan_(NULL), initialized_(false) { + + Globals* simParams = info_->getSimParams(); + fqParams_ = simParams->getFluctuatingChargeParameters(); + } + FluctuatingChargePropagator::~FluctuatingChargePropagator() { + } + + void FluctuatingChargePropagator::setForceManager(ForceManager* forceMan) { + forceMan_ = forceMan; + } + + void FluctuatingChargePropagator::initialize() { + if (info_->usesFluctuatingCharges()) { + if (info_->getNFluctuatingCharges() > 0) { + hasFlucQ_ = true; + fqConstraints_ = new FluctuatingChargeConstraints(info_); + fqConstraints_->setConstrainRegions(fqParams_->getConstrainRegions()); + } + } + + if (!hasFlucQ_) { + initialized_ = true; + return; + } + SimInfo::MoleculeIterator i; Molecule::FluctuatingChargeIterator j; Molecule* mol; Atom* atom; - RealType totalFrc, totalMolFrc, constrainedFrc; + // For single-minima flucq, this ensures a net neutral system, but + // for multiple minima, this is no longer the right thing to do: + // + // for (mol = info_->beginMolecule(i); mol != NULL; + // mol = info_->nextMolecule(i)) { + // for (atom = mol->beginFluctuatingCharge(j); atom != NULL; + // atom = mol->nextFluctuatingCharge(j)) { + // atom->setFlucQPos(0.0); + // atom->setFlucQVel(0.0); + // } + // } + + FluctuatingChargeObjectiveFunction flucQobjf(info_, forceMan_, + fqConstraints_); - // accumulate the total system fluctuating charge forces - totalFrc = 0.0; + DynamicVector initCoords = flucQobjf.setInitialCoords(); + Problem problem(flucQobjf, *(new NoConstraint()), *(new NoStatus()), + initCoords); - for (mol = info_->beginMolecule(i); mol != NULL; - mol = info_->nextMolecule(i)) { + EndCriteria endCriteria(1000, 100, 1e-5, 1e-5, 1e-5); - for (atom = mol->beginFluctuatingCharge(j); atom != NULL; - atom = mol->nextFluctuatingCharge(j)) { - totalFrc += atom->getFlucQFrc(); - } + OptimizationMethod* minim = OptimizationFactory::getInstance()->createOptimization("SD", info_); - } + DumpStatusFunction dsf(info_); // we want a dump file written + // every iteration + minim->minimize(problem, endCriteria); + cerr << "back from minim\n"; + initialized_ = true; + } -#ifdef IS_MPI - // in parallel, we need to add up the contributions from all - // processors: - MPI::COMM_WORLD.Allreduce(MPI::IN_PLACE, &totalFrc, 1, MPI::REALTYPE, - MPI::SUM); -#endif - - // divide by the total number of fluctuating charges: - totalFrc /= info_->getNFluctuatingCharges(); - - for (mol = info_->beginMolecule(i); mol != NULL; - mol = info_->nextMolecule(i)) { - - totalMolFrc = 0.0; - - // molecular constraints can be done with a second loop. - if (mol->constrainTotalCharge()) { - for (atom = mol->beginFluctuatingCharge(j); atom != NULL; - atom = mol->nextFluctuatingCharge(j)) { - totalMolFrc += atom->getFlucQFrc(); - } - totalMolFrc /= mol->getNFluctuatingCharges(); - } - - for (atom = mol->beginFluctuatingCharge(j); atom != NULL; - atom = mol->nextFluctuatingCharge(j)) { - constrainedFrc = atom->getFlucQFrc() - totalFrc - totalMolFrc; - atom->setFlucQFrc(constrainedFrc); - } - } + void FluctuatingChargePropagator::applyConstraints() { + if (!initialized_) initialize(); + if (!hasFlucQ_) return; + fqConstraints_->applyConstraints(); } }