Search in sources :

Example 16 with EnergyException

use of ffx.potential.utils.EnergyException in project ffx by mjschnie.

the class ParticleMeshEwaldCart method scfByPCG.

private int scfByPCG(boolean print, long startTime) {
    long directTime = System.nanoTime() - startTime;
    /**
     * A request of 0 SCF cycles simplifies mutual polarization to direct
     * polarization.
     */
    StringBuilder sb = null;
    if (print) {
        sb = new StringBuilder("\n Self-Consistent Field\n" + " Iter  RMS Change (Debye)  Time\n");
    }
    /**
     * Find the induced dipole field due to direct dipoles (or predicted
     * induced dipoles from previous steps).
     */
    computeInduceDipoleField();
    try {
        /**
         * Set initial conjugate gradient residual (a field).
         *
         * Store the current induced dipoles and load the residual induced
         * dipole
         */
        parallelTeam.execute(pcgInitRegion1);
        /**
         * Compute preconditioner.
         */
        if (nSymm > 1) {
            parallelTeam.execute(expandInducedDipolesRegion);
        }
        parallelTeam.execute(inducedDipolePreconditionerRegion);
        /**
         * Revert to the stored induce dipoles.
         *
         * Set initial conjugate vector (induced dipoles).
         */
        parallelTeam.execute(pcgInitRegion2);
    } catch (Exception e) {
        String message = "Exception initializing preconditioned CG.";
        logger.log(Level.SEVERE, message, e);
    }
    /**
     * Conjugate gradient iteration of the mutual induced dipoles.
     */
    int completedSCFCycles = 0;
    int maxSCFCycles = 1000;
    double eps = 100.0;
    double previousEps;
    boolean done = false;
    while (!done) {
        long cycleTime = -System.nanoTime();
        /**
         * Store a copy of the current induced dipoles, then set the induced
         * dipoles to the conjugate vector.
         */
        for (int i = 0; i < nAtoms; i++) {
            vec[0][i] = inducedDipole[0][i][0];
            vec[1][i] = inducedDipole[0][i][1];
            vec[2][i] = inducedDipole[0][i][2];
            inducedDipole[0][i][0] = conj[0][i];
            inducedDipole[0][i][1] = conj[1][i];
            inducedDipole[0][i][2] = conj[2][i];
            vecCR[0][i] = inducedDipoleCR[0][i][0];
            vecCR[1][i] = inducedDipoleCR[0][i][1];
            vecCR[2][i] = inducedDipoleCR[0][i][2];
            inducedDipoleCR[0][i][0] = conjCR[0][i];
            inducedDipoleCR[0][i][1] = conjCR[1][i];
            inducedDipoleCR[0][i][2] = conjCR[2][i];
        }
        /**
         * Find the induced dipole field.
         */
        computeInduceDipoleField();
        try {
            /**
             * Revert the induced dipoles to the saved values, then save the
             * new residual field.
             *
             * Compute dot product of the conjugate vector and new residual.
             *
             * Reduce the residual field, add to the induced dipoles based
             * on the scaled conjugate vector and finally set the induced
             * dipoles to the polarizability times the residual field.
             */
            parallelTeam.execute(pcgIterRegion1);
            /**
             * Compute preconditioner.
             */
            if (nSymm > 1) {
                parallelTeam.execute(expandInducedDipolesRegion);
            }
            parallelTeam.execute(inducedDipolePreconditionerRegion);
            /**
             * Revert the induced dipoles to the saved values.
             *
             * Compute the dot product of the residual and preconditioner.
             *
             * Update the conjugate vector and sum the square of the
             * residual field.
             */
            pcgIterRegion2.sum = pcgIterRegion1.sumShared.get();
            pcgIterRegion2.sumCR = pcgIterRegion1.sumCRShared.get();
            parallelTeam.execute(pcgIterRegion2);
        } catch (Exception e) {
            String message = "Exception in first CG iteration region.";
            logger.log(Level.SEVERE, message, e);
        }
        previousEps = eps;
        // eps = max(eps, epsCR);
        eps = max(pcgIterRegion2.epsShared.get(), pcgIterRegion2.epsCRShared.get());
        completedSCFCycles++;
        eps = MultipoleType.DEBYE * sqrt(eps / (double) nAtoms);
        cycleTime += System.nanoTime();
        if (print) {
            sb.append(format(" %4d     %15.10f %7.4f\n", completedSCFCycles, eps, cycleTime * TO_SECONDS));
        }
        /**
         * If the RMS Debye change increases, fail the SCF process.
         */
        if (eps > previousEps) {
            if (sb != null) {
                logger.warning(sb.toString());
            }
            String message = format("Fatal SCF convergence failure: (%10.5f > %10.5f)\n", eps, previousEps);
            throw new EnergyException(message, false);
        }
        /**
         * The SCF should converge well before the max iteration check.
         * Otherwise, fail the SCF process.
         */
        if (completedSCFCycles >= maxSCFCycles) {
            if (sb != null) {
                logger.warning(sb.toString());
            }
            String message = format("Maximum SCF iterations reached: (%d)\n", completedSCFCycles);
            throw new EnergyException(message, false);
        }
        /**
         * Check if the convergence criteria has been achieved.
         */
        if (eps < poleps) {
            done = true;
        }
    }
    if (print) {
        sb.append(format(" Direct:                  %7.4f\n", TO_SECONDS * directTime));
        startTime = System.nanoTime() - startTime;
        sb.append(format(" Total:                   %7.4f", startTime * TO_SECONDS));
        logger.info(sb.toString());
    /**
     * for (int i = 0; i < nAtoms; i++) { double dx =
     * MultipoleType.DEBYE * inducedDipole[0][i][0]; double dy =
     * MultipoleType.DEBYE * inducedDipole[0][i][1]; double dz =
     * MultipoleType.DEBYE * inducedDipole[0][i][2]; double norm =
     * sqrt(dx * dx + dy * dy + dz * dz); logger.info(format("ind %d
     * %16.6f %16.6f %16.6f %16.6f", i + 1, dx, dy, dz, norm)); dx =
     * MultipoleType.DEBYE * inducedDipoleCR[0][i][0]; dy =
     * MultipoleType.DEBYE * inducedDipoleCR[0][i][1]; dz =
     * MultipoleType.DEBYE * inducedDipoleCR[0][i][2]; norm = sqrt(dx *
     * dx + dy * dy + dz * dz); logger.info(format("inp %d %16.6f %16.6f
     * %16.6f %16.6f", i + 1, dx, dy, dz, norm)); }
     */
    }
    /**
     * Find the final induced dipole field.
     */
    computeInduceDipoleField();
    return completedSCFCycles;
}
Also used : ForceFieldString(ffx.potential.parameters.ForceField.ForceFieldString) EnergyException(ffx.potential.utils.EnergyException) EnergyException(ffx.potential.utils.EnergyException)

Example 17 with EnergyException

use of ffx.potential.utils.EnergyException in project ffx by mjschnie.

the class ParticleMeshEwaldQI method scfBySOR.

/**
 * Converge the SCF using Successive Over-Relaxation (SOR).
 */
private int scfBySOR(boolean print, long startTime) {
    long directTime = System.nanoTime() - startTime;
    /**
     * A request of 0 SCF cycles simplifies mutual polarization to direct
     * polarization.
     */
    StringBuilder sb = null;
    if (print) {
        sb = new StringBuilder("\n Self-Consistent Field\n" + " Iter  RMS Change (Debye)  Time\n");
    }
    int completedSCFCycles = 0;
    int maxSCFCycles = 1000;
    double eps = 100.0;
    double previousEps;
    boolean done = false;
    while (!done) {
        long cycleTime = -System.nanoTime();
        try {
            if (reciprocalSpaceTerm && aewald > 0.0) {
                reciprocalSpace.splineInducedDipoles(inducedDipole, inducedDipoleCR, use);
            }
            sectionTeam.execute(inducedDipoleFieldRegion);
            if (reciprocalSpaceTerm && aewald > 0.0) {
                reciprocalSpace.computeInducedPhi(cartesianDipolePhi, cartesianDipolePhiCR);
            }
            if (generalizedKirkwoodTerm) {
                /**
                 * GK field.
                 */
                gkEnergyTotal = -System.nanoTime();
                generalizedKirkwood.computeInducedGKField();
                gkEnergyTotal += System.nanoTime();
                logger.fine(format(" Computed GK induced field %8.3f (sec)", gkEnergyTotal * 1.0e-9));
            }
            parallelTeam.execute(sorRegion);
            if (nSymm > 1) {
                parallelTeam.execute(expandInducedDipolesRegion);
            }
        } catch (RuntimeException ex) {
            logger.warning("Exception computing mutual induced dipoles.");
            throw ex;
        } catch (Exception ex) {
            logger.log(Level.SEVERE, "Exception computing mutual induced dipoles.", ex);
        }
        completedSCFCycles++;
        previousEps = eps;
        eps = sorRegion.getEps();
        eps = MultipoleType.DEBYE * sqrt(eps / (double) nAtoms);
        cycleTime += System.nanoTime();
        if (print) {
            sb.append(format(" %4d     %15.10f %7.4f\n", completedSCFCycles, eps, cycleTime * TO_SECONDS));
        }
        /**
         * If the RMS Debye change increases, fail the SCF process.
         */
        if (eps > previousEps) {
            if (sb != null) {
                logger.warning(sb.toString());
            }
            String message = format("Fatal SCF convergence failure: (%10.5f > %10.5f)\n", eps, previousEps);
            throw new EnergyException(message, false);
        }
        /**
         * The SCF should converge well before the max iteration check.
         * Otherwise, fail the SCF process.
         */
        if (completedSCFCycles >= maxSCFCycles) {
            if (sb != null) {
                logger.warning(sb.toString());
            }
            String message = format("Maximum SCF iterations reached: (%d)\n", completedSCFCycles);
            throw new EnergyException(message, false);
        }
        /**
         * Check if the convergence criteria has been achieved.
         */
        if (eps < poleps) {
            done = true;
        }
    }
    if (print) {
        sb.append(format(" Direct:                  %7.4f\n", TO_SECONDS * directTime));
        startTime = System.nanoTime() - startTime;
        sb.append(format(" Total:                   %7.4f", startTime * TO_SECONDS));
        logger.info(sb.toString());
    }
    return completedSCFCycles;
}
Also used : ForceFieldString(ffx.potential.parameters.ForceField.ForceFieldString) EnergyException(ffx.potential.utils.EnergyException) EnergyException(ffx.potential.utils.EnergyException)

Example 18 with EnergyException

use of ffx.potential.utils.EnergyException in project ffx by mjschnie.

the class ParticleMeshEwaldQI method scfByPCG.

private int scfByPCG(boolean print, long startTime) {
    long directTime = System.nanoTime() - startTime;
    /**
     * A request of 0 SCF cycles simplifies mutual polarization to direct
     * polarization.
     */
    StringBuilder sb = null;
    if (print) {
        sb = new StringBuilder("\n Self-Consistent Field\n" + " Iter  RMS Change (Debye)  Time\n");
    }
    /**
     * Find the induced dipole field due to direct dipoles (or predicted
     * induced dipoles from previous steps).
     */
    computeInduceDipoleField();
    try {
        /**
         * Set initial conjugate gradient residual (a field).
         *
         * Store the current induced dipoles and load the residual induced
         * dipole
         */
        parallelTeam.execute(pcgInitRegion1);
        /**
         * Compute preconditioner.
         */
        if (nSymm > 1) {
            parallelTeam.execute(expandInducedDipolesRegion);
        }
        parallelTeam.execute(inducedDipolePreconditionerRegion);
        /**
         * Revert to the stored induce dipoles.
         *
         * Set initial conjugate vector (induced dipoles).
         */
        parallelTeam.execute(pcgInitRegion2);
    } catch (Exception e) {
        String message = "Exception initializing preconditioned CG.";
        logger.log(Level.SEVERE, message, e);
    }
    /**
     * Conjugate gradient iteration of the mutual induced dipoles.
     */
    int completedSCFCycles = 0;
    int maxSCFCycles = 1000;
    double eps = 100.0;
    double previousEps;
    boolean done = false;
    while (!done) {
        long cycleTime = -System.nanoTime();
        /**
         * Store a copy of the current induced dipoles, then set the induced
         * dipoles to the conjugate vector.
         */
        for (int i = 0; i < nAtoms; i++) {
            vec[0][i] = inducedDipole[0][i][0];
            vec[1][i] = inducedDipole[0][i][1];
            vec[2][i] = inducedDipole[0][i][2];
            inducedDipole[0][i][0] = conj[0][i];
            inducedDipole[0][i][1] = conj[1][i];
            inducedDipole[0][i][2] = conj[2][i];
            vecCR[0][i] = inducedDipoleCR[0][i][0];
            vecCR[1][i] = inducedDipoleCR[0][i][1];
            vecCR[2][i] = inducedDipoleCR[0][i][2];
            inducedDipoleCR[0][i][0] = conjCR[0][i];
            inducedDipoleCR[0][i][1] = conjCR[1][i];
            inducedDipoleCR[0][i][2] = conjCR[2][i];
        }
        /**
         * Find the induced dipole field.
         */
        computeInduceDipoleField();
        try {
            /**
             * Revert the induced dipoles to the saved values, then save the
             * new residual field.
             *
             * Compute dot product of the conjugate vector and new residual.
             *
             * Reduce the residual field, add to the induced dipoles based
             * on the scaled conjugate vector and finally set the induced
             * dipoles to the polarizability times the residual field.
             */
            parallelTeam.execute(pcgIterRegion1);
            /**
             * Compute preconditioner.
             */
            if (nSymm > 1) {
                parallelTeam.execute(expandInducedDipolesRegion);
            }
            parallelTeam.execute(inducedDipolePreconditionerRegion);
            /**
             * Revert the induced dipoles to the saved values.
             *
             * Compute the dot product of the residual and preconditioner.
             *
             * Update the conjugate vector and sum the square of the
             * residual field.
             */
            pcgIterRegion2.sum = pcgIterRegion1.sumShared.get();
            pcgIterRegion2.sumCR = pcgIterRegion1.sumCRShared.get();
            parallelTeam.execute(pcgIterRegion2);
        } catch (Exception e) {
            String message = "Exception in first CG iteration region.";
            logger.log(Level.SEVERE, message, e);
        }
        previousEps = eps;
        // eps = max(eps, epsCR);
        eps = max(pcgIterRegion2.epsShared.get(), pcgIterRegion2.epsCRShared.get());
        completedSCFCycles++;
        eps = MultipoleType.DEBYE * sqrt(eps / (double) nAtoms);
        cycleTime += System.nanoTime();
        if (print) {
            sb.append(format(" %4d     %15.10f %7.4f\n", completedSCFCycles, eps, cycleTime * TO_SECONDS));
        }
        /**
         * If the RMS Debye change increases, fail the SCF process.
         */
        if (eps > previousEps) {
            if (sb != null) {
                logger.warning(sb.toString());
            }
            String message = format("Fatal SCF convergence failure: (%10.5f > %10.5f)\n", eps, previousEps);
            throw new EnergyException(message, false);
        }
        /**
         * The SCF should converge well before the max iteration check.
         * Otherwise, fail the SCF process.
         */
        if (completedSCFCycles >= maxSCFCycles) {
            if (sb != null) {
                logger.warning(sb.toString());
            }
            String message = format("Maximum SCF iterations reached: (%d)\n", completedSCFCycles);
            throw new EnergyException(message, false);
        }
        /**
         * Check if the convergence criteria has been achieved.
         */
        if (eps < poleps) {
            done = true;
        }
    }
    if (print) {
        sb.append(format(" Direct:                  %7.4f\n", TO_SECONDS * directTime));
        startTime = System.nanoTime() - startTime;
        sb.append(format(" Total:                   %7.4f", startTime * TO_SECONDS));
        logger.info(sb.toString());
    }
    /**
     * Find the final induced dipole field.
     */
    computeInduceDipoleField();
    return completedSCFCycles;
}
Also used : ForceFieldString(ffx.potential.parameters.ForceField.ForceFieldString) EnergyException(ffx.potential.utils.EnergyException) EnergyException(ffx.potential.utils.EnergyException)

Aggregations

EnergyException (ffx.potential.utils.EnergyException)18 ForceFieldString (ffx.potential.parameters.ForceField.ForceFieldString)6 CoordRestraint (ffx.potential.nonbonded.CoordRestraint)4 PotentialsFunctions (ffx.potential.utils.PotentialsFunctions)4 PotentialsUtils (ffx.potential.utils.PotentialsUtils)4 File (java.io.File)4 Atom (ffx.potential.bonded.Atom)3 COMRestraint (ffx.potential.nonbonded.COMRestraint)3 NCSRestraint (ffx.potential.nonbonded.NCSRestraint)3 Crystal (ffx.crystal.Crystal)2 ForceFieldEnergy (ffx.potential.ForceFieldEnergy)2 Respa (ffx.algorithms.integrators.Respa)1 Stochastic (ffx.algorithms.integrators.Stochastic)1 MultiResidue (ffx.potential.bonded.MultiResidue)1 Residue (ffx.potential.bonded.Residue)1 OpenMM_System_addConstraint (simtk.openmm.OpenMMLibrary.OpenMM_System_addConstraint)1 OpenMM_Vec3 (simtk.openmm.OpenMM_Vec3)1