Search in sources :

Example 11 with ForceFieldEnergy

use of ffx.potential.ForceFieldEnergy in project ffx by mjschnie.

the class TitrationUtils method titratingMultiresidueFactory.

/**
 * Create a MultiResidue from the given Residue by adding its alternated
 * protonation state(s) as alternate possibilities.
 */
public static MultiResidue titratingMultiresidueFactory(MolecularAssembly mola, Residue res) {
    ForceField ff = mola.getForceField();
    Potential potential = mola.getPotentialEnergy();
    if (!(potential instanceof ForceFieldEnergy)) {
        logger.warning(String.format("TitrationFactory only supported by ForceFieldEnergy potentials."));
        throw new IllegalStateException();
    }
    ForceFieldEnergy ffe = (ForceFieldEnergy) potential;
    /* Create new titration state. */
    Titration titration = Titration.lookup(res);
    String targetName = (titration.protForm != res.getAminoAcid3()) ? titration.protForm.toString() : titration.deprotForm.toString();
    int resNumber = res.getResidueNumber();
    Residue.ResidueType resType = res.getResidueType();
    Residue newRes = new Residue(targetName, resNumber, resType);
    /* Wrap both states in a MultiResidue. */
    MultiResidue multiRes = new MultiResidue(res, ff, ffe);
    Polymer polymer = findResiduePolymer(res, mola);
    polymer.addMultiResidue(multiRes);
    multiRes.addResidue(newRes);
    /* Begin in protonated state by default. */
    multiRes.setActiveResidue(titration.protForm);
    propagateInactiveResidues(multiRes, false);
    ffe.reInit();
    return multiRes;
}
Also used : MultiResidue(ffx.potential.bonded.MultiResidue) Residue(ffx.potential.bonded.Residue) Polymer(ffx.potential.bonded.Polymer) ForceField(ffx.potential.parameters.ForceField) ForceFieldEnergy(ffx.potential.ForceFieldEnergy) Potential(ffx.numerics.Potential) MultiResidue(ffx.potential.bonded.MultiResidue)

Example 12 with ForceFieldEnergy

use of ffx.potential.ForceFieldEnergy in project ffx by mjschnie.

the class RefinementEnergy method energyAndGradient.

/**
 * {@inheritDoc}
 *
 * Implementation of the {@link Potential} interface for the
 * RefinementEnergy.
 */
@Override
public double energyAndGradient(double[] x, double[] g) {
    double weight = data.getWeight();
    double e = 0.0;
    fill(g, 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 and gradient.
            for (int i = 0; i < assemblysize; i++) {
                try {
                    ForceFieldEnergy fe = molecularAssemblies[i].getPotentialEnergy();
                    getAssemblyi(i, x, xChemical[i]);
                    double curE = fe.energyAndGradient(xChemical[i], gChemical[i]);
                    e += (curE - e) / (i + 1);
                    setAssemblyi(i, g, gChemical[i]);
                } 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;
            // normalize gradients for multiple-counted atoms
            if (assemblysize > 1) {
                for (int i = 0; i < nXYZ; i++) {
                    g[i] /= assemblysize;
                }
            }
            for (int i = 0; i < nXYZ; i++) {
                g[i] *= kTScale;
            }
            // Compute the X-ray target energy and gradient.
            if (gXray == null || gXray.length != nXYZ) {
                gXray = new double[nXYZ];
            }
            double xE = dataEnergy.energyAndGradient(x, gXray);
            // System.out.println("Xray E: " + xE + " scaled Xray E: " + weight * xE);
            e += weight * xE;
            // Add the chemical and X-ray gradients.
            for (int i = 0; i < nXYZ; i++) {
                g[i] += weight * gXray[i];
            }
            break;
        case BFACTORS:
        case OCCUPANCIES:
        case BFACTORS_AND_OCCUPANCIES:
            // Compute the X-ray target energy and gradient.
            e = dataEnergy.energyAndGradient(x, g);
            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.energyAndGradient(xChemical[i], gChemical[i]);
                    e += (curE - e) / (i + 1);
                    setAssemblyi(i, g, gChemical[i]);
                } 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;
                }
            }
            // normalize gradients for multiple-counted atoms
            if (assemblysize > 1) {
                for (int i = 0; i < nXYZ; i++) {
                    g[i] /= assemblysize;
                }
            }
            // Compute the X-ray target energy and gradient.
            if (gXray == null || gXray.length != n) {
                gXray = new double[n];
            }
            e += weight * dataEnergy.energyAndGradient(x, gXray);
            // Add the chemical and X-ray gradients.
            for (int i = 0; i < nXYZ; i++) {
                g[i] += weight * gXray[i];
            }
            // bfactors, occ
            if (n > nXYZ) {
                for (int i = nXYZ; i < n; i++) {
                    g[i] = weight * gXray[i];
                }
            }
            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];
            g[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)

Example 13 with ForceFieldEnergy

use of ffx.potential.ForceFieldEnergy 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)

Example 14 with ForceFieldEnergy

use of ffx.potential.ForceFieldEnergy in project ffx by mjschnie.

the class RefinementEnergy method setEnergyTermState.

@Override
public void setEnergyTermState(STATE state) {
    this.state = state;
    int assemblysize = molecularAssemblies.length;
    for (int i = 0; i < assemblysize; i++) {
        ForceFieldEnergy fe = molecularAssemblies[i].getPotentialEnergy();
        fe.setEnergyTermState(state);
    }
    dataEnergy.setEnergyTermState(state);
}
Also used : ForceFieldEnergy(ffx.potential.ForceFieldEnergy)

Example 15 with ForceFieldEnergy

use of ffx.potential.ForceFieldEnergy in project ffx by mjschnie.

the class RefinementEnergy method getdEdXdL.

/**
 * {@inheritDoc}
 */
@Override
public void getdEdXdL(double[] gradient) {
    double weight = data.getWeight();
    if (thermostat != null) {
        kTScale = Thermostat.convert / (thermostat.getTargetTemperature() * Thermostat.kB);
    }
    int assemblysize = molecularAssemblies.length;
    /**
     * Compute the chemical energy and gradient.
     */
    for (int i = 0; i < assemblysize; i++) {
        ForceFieldEnergy forcefieldEnergy = molecularAssemblies[i].getPotentialEnergy();
        Arrays.fill(gChemical[i], 0.0);
        forcefieldEnergy.getdEdXdL(gChemical[i]);
    }
    for (int i = 0; i < assemblysize; i++) {
        for (int j = 0; j < nXYZ; j++) {
            gradient[j] += gChemical[i][j];
        }
    }
    /**
     * Normalize gradients for multiple-counted atoms.
     */
    if (assemblysize > 1) {
        for (int i = 0; i < nXYZ; i++) {
            gradient[i] /= assemblysize;
        }
    }
    for (int i = 0; i < nXYZ; i++) {
        gradient[i] *= kTScale;
    }
    /**
     * Compute the X-ray target energy and gradient.
     */
    if (gXray == null || gXray.length != nXYZ) {
        gXray = new double[nXYZ];
    } else {
        for (int j = 0; j < nXYZ; j++) {
            gXray[j] = 0.0;
        }
    }
    if (data instanceof DiffractionData) {
        XRayEnergy xRayEnergy = (XRayEnergy) dataEnergy;
        xRayEnergy.getdEdXdL(gXray);
    } else if (data instanceof RealSpaceData) {
        RealSpaceEnergy realSpaceEnergy = (RealSpaceEnergy) dataEnergy;
        realSpaceEnergy.getdEdXdL(gXray);
    }
    // Add the chemical and X-ray gradients.
    for (int i = 0; i < nXYZ; i++) {
        gradient[i] += weight * gXray[i];
    }
}
Also used : RealSpaceData(ffx.realspace.RealSpaceData) ForceFieldEnergy(ffx.potential.ForceFieldEnergy) RealSpaceEnergy(ffx.realspace.RealSpaceEnergy)

Aggregations

ForceFieldEnergy (ffx.potential.ForceFieldEnergy)25 MolecularAssembly (ffx.potential.MolecularAssembly)11 File (java.io.File)6 ForceField (ffx.potential.parameters.ForceField)5 CompositeConfiguration (org.apache.commons.configuration.CompositeConfiguration)5 ForceFieldFilter (ffx.potential.parsers.ForceFieldFilter)4 PDBFilter (ffx.potential.parsers.PDBFilter)4 ParallelTeam (edu.rit.pj.ParallelTeam)3 Crystal (ffx.crystal.Crystal)3 ReflectionList (ffx.crystal.ReflectionList)3 Resolution (ffx.crystal.Resolution)3 Atom (ffx.potential.bonded.Atom)3 ExtendedSystem (ffx.potential.extended.ExtendedSystem)3 PotentialsUtils (ffx.potential.utils.PotentialsUtils)3 RealSpaceData (ffx.realspace.RealSpaceData)3 RealSpaceEnergy (ffx.realspace.RealSpaceEnergy)3 Test (org.junit.Test)3 HKL (ffx.crystal.HKL)2 ComplexNumber (ffx.numerics.ComplexNumber)2 ExtendedSystemConfig (ffx.potential.extended.ExtendedSystem.ExtendedSystemConfig)2