Search in sources :

Example 6 with PotentialsFunctions

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

the class RefinementEnergy method energy.

@Override
public double energy(double[] x) {
    double weight = data.getWeight();
    double e = 0.0;
    if (thermostat != null) {
        kTScale = Thermostat.convert / (thermostat.getTargetTemperature() * Thermostat.kB);
    }
    if (optimizationScaling != null) {
        int len = x.length;
        for (int i = 0; i < len; i++) {
            x[i] /= optimizationScaling[i];
        }
    }
    int assemblysize = molecularAssemblies.length;
    switch(refinementMode) {
        case COORDINATES:
            // Compute the chemical energy.
            for (int i = 0; i < assemblysize; i++) {
                try {
                    ForceFieldEnergy fe = molecularAssemblies[i].getPotentialEnergy();
                    getAssemblyi(i, x, xChemical[i]);
                    double curE = fe.energy(xChemical[i]);
                    e += (curE - e) / (i + 1);
                } catch (EnergyException ex) {
                    ex.printStackTrace();
                    if (printOnFailure) {
                        String timeString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss"));
                        String filename = String.format("%s-ERROR-%s.pdb", FilenameUtils.removeExtension(molecularAssemblies[i].getFile().getName()), timeString);
                        PotentialsFunctions ef = new PotentialsUtils();
                        filename = ef.versionFile(filename);
                        logger.info(String.format(" Writing on-error snapshot to file %s", filename));
                        ef.saveAsPDB(molecularAssemblies[i], new File(filename));
                    }
                    if (ex.doCauseSevere()) {
                        ex.printStackTrace();
                        logger.log(Level.SEVERE, " Error in calculating energies or gradients", ex);
                    } else {
                        ex.printStackTrace();
                        // Rethrow exception
                        throw ex;
                    }
                    // Should ordinarily be unreachable.
                    return 0;
                }
            }
            double chemE = e;
            e = chemE * kTScale;
            // Compute the X-ray target energy.
            double xE = dataEnergy.energy(x);
            e += weight * xE;
            break;
        case BFACTORS:
        case OCCUPANCIES:
        case BFACTORS_AND_OCCUPANCIES:
            // Compute the X-ray target energy and gradient.
            e = dataEnergy.energy(x);
            break;
        case COORDINATES_AND_BFACTORS:
        case COORDINATES_AND_OCCUPANCIES:
        case COORDINATES_AND_BFACTORS_AND_OCCUPANCIES:
            // Compute the chemical energy and gradient.
            for (int i = 0; i < assemblysize; i++) {
                try {
                    ForceFieldEnergy fe = molecularAssemblies[i].getPotentialEnergy();
                    getAssemblyi(i, x, xChemical[i]);
                    double curE = fe.energy(xChemical[i]);
                    e += (curE - e) / (i + 1);
                } catch (EnergyException ex) {
                    ex.printStackTrace();
                    if (printOnFailure) {
                        String timeString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd-HH_mm_ss"));
                        String filename = String.format("%s-ERROR-%s.pdb", FilenameUtils.removeExtension(molecularAssemblies[i].getFile().getName()), timeString);
                        PotentialsFunctions ef = new PotentialsUtils();
                        filename = ef.versionFile(filename);
                        logger.info(String.format(" Writing on-error snapshot to file %s", filename));
                        ef.saveAsPDB(molecularAssemblies[i], new File(filename));
                    }
                    if (ex.doCauseSevere()) {
                        ex.printStackTrace();
                        logger.log(Level.SEVERE, " Error in calculating energies or gradients", ex);
                    } else {
                        ex.printStackTrace();
                        // Rethrow exception
                        throw ex;
                    }
                    // Should ordinarily be unreachable.
                    return 0;
                }
            }
            e += weight * dataEnergy.energy(x);
            break;
        default:
            String message = "Unknown refinement mode.";
            logger.log(Level.SEVERE, message);
    }
    if (optimizationScaling != null) {
        int len = x.length;
        for (int i = 0; i < len; i++) {
            x[i] *= optimizationScaling[i];
        }
    }
    totalEnergy = e;
    return e;
}
Also used : PotentialsFunctions(ffx.potential.utils.PotentialsFunctions) ForceFieldEnergy(ffx.potential.ForceFieldEnergy) File(java.io.File) EnergyException(ffx.potential.utils.EnergyException) PotentialsUtils(ffx.potential.utils.PotentialsUtils)

Aggregations

PotentialsFunctions (ffx.potential.utils.PotentialsFunctions)6 PotentialsUtils (ffx.potential.utils.PotentialsUtils)6 File (java.io.File)6 EnergyException (ffx.potential.utils.EnergyException)4 CoordRestraint (ffx.potential.nonbonded.CoordRestraint)3 ForceFieldEnergy (ffx.potential.ForceFieldEnergy)2 COMRestraint (ffx.potential.nonbonded.COMRestraint)2 NCSRestraint (ffx.potential.nonbonded.NCSRestraint)2 ForceFieldString (ffx.potential.parameters.ForceField.ForceFieldString)2 PointerByReference (com.sun.jna.ptr.PointerByReference)1 Atom (ffx.potential.bonded.Atom)1 MultiResidue (ffx.potential.bonded.MultiResidue)1 Residue (ffx.potential.bonded.Residue)1 OpenMM_System_addConstraint (simtk.openmm.OpenMMLibrary.OpenMM_System_addConstraint)1