Search in sources :

Example 1 with OpenMM_Vec3

use of simtk.openmm.OpenMM_Vec3 in project ffx by mjschnie.

the class ForceFieldEnergyOpenMM method getOpenMMVelocities.

/**
 * getOpenMMVelocities takes in a PointerByReference containing the velocity
 * information of the context. This method creates a Vec3Array that contains
 * the three dimensional information of the velocities of the atoms. This
 * method then adds these values to a new double array v and returns it to
 * the method call
 *
 * @param velocities
 * @param numberOfVariables
 * @return
 */
public double[] getOpenMMVelocities(PointerByReference velocities, int numberOfVariables, double[] v) {
    assert numberOfVariables == getNumberOfVariables();
    if (v == null || v.length < numberOfVariables) {
        v = new double[numberOfVariables];
    }
    Atom[] atoms = molecularAssembly.getAtomArray();
    int nAtoms = atoms.length;
    for (int i = 0; i < nAtoms; i++) {
        int offset = i * 3;
        OpenMM_Vec3 vel = OpenMM_Vec3Array_get(velocities, i);
        v[offset] = vel.x * OpenMM_AngstromsPerNm;
        v[offset + 1] = vel.y * OpenMM_AngstromsPerNm;
        v[offset + 2] = vel.z * OpenMM_AngstromsPerNm;
        Atom atom = atoms[i];
        double[] velocity = { v[offset], v[offset + 1], v[offset + 2] };
        atom.setVelocity(velocity);
    }
    return v;
}
Also used : OpenMM_Vec3(simtk.openmm.OpenMM_Vec3) Atom(ffx.potential.bonded.Atom) OpenMM_System_addConstraint(simtk.openmm.OpenMMLibrary.OpenMM_System_addConstraint) CoordRestraint(ffx.potential.nonbonded.CoordRestraint)

Example 2 with OpenMM_Vec3

use of simtk.openmm.OpenMM_Vec3 in project ffx by mjschnie.

the class ForceFieldEnergyOpenMM method getOpenMMPositions.

/**
 * getOpenMMPositions takes in a PointerByReference containing the position
 * information of the context. This method creates a Vec3Array that contains
 * the three dimensional information of the positions of the atoms. The
 * method then adds these values to a new double array x and returns it to
 * the method call
 *
 * @param positions
 * @param numberOfVariables
 * @param x
 * @return x
 */
public double[] getOpenMMPositions(PointerByReference positions, int numberOfVariables, double[] x) {
    assert numberOfVariables == getNumberOfVariables();
    if (x == null || x.length < numberOfVariables) {
        x = new double[numberOfVariables];
    }
    Atom[] atoms = molecularAssembly.getAtomArray();
    int nAtoms = atoms.length;
    for (int i = 0; i < nAtoms; i++) {
        int offset = i * 3;
        OpenMM_Vec3 pos = OpenMM_Vec3Array_get(positions, i);
        x[offset] = pos.x * OpenMM_AngstromsPerNm;
        x[offset + 1] = pos.y * OpenMM_AngstromsPerNm;
        x[offset + 2] = pos.z * OpenMM_AngstromsPerNm;
        Atom atom = atoms[i];
        atom.moveTo(x[offset], x[offset + 1], x[offset + 2]);
    }
    return x;
}
Also used : OpenMM_Vec3(simtk.openmm.OpenMM_Vec3) Atom(ffx.potential.bonded.Atom) OpenMM_System_addConstraint(simtk.openmm.OpenMMLibrary.OpenMM_System_addConstraint) CoordRestraint(ffx.potential.nonbonded.CoordRestraint)

Example 3 with OpenMM_Vec3

use of simtk.openmm.OpenMM_Vec3 in project ffx by mjschnie.

the class ForceFieldEnergyOpenMM method setOpenMMVelocities.

/**
 * setOpenMMVelocities takes in an array of doubles generated by the DYN
 * reader method and appends these values to a Vec3Array. Finally this
 * method sets the created Vec3Arrat as the velocities of the context.
 *
 * @param v
 * @param numberOfVariables
 */
public void setOpenMMVelocities(double[] v, int numberOfVariables) {
    assert numberOfVariables == getNumberOfVariables();
    if (velocities == null) {
        velocities = OpenMM_Vec3Array_create(0);
    } else {
        OpenMM_Vec3Array_resize(velocities, 0);
    }
    OpenMM_Vec3.ByValue vel = new OpenMM_Vec3.ByValue();
    for (int i = 0; i < numberOfVariables; i = i + 3) {
        vel.x = v[i] * OpenMM_NmPerAngstrom;
        vel.y = v[i + 1] * OpenMM_NmPerAngstrom;
        vel.z = v[i + 2] * OpenMM_NmPerAngstrom;
        OpenMM_Vec3Array_append(velocities, vel);
    }
    OpenMM_Context_setVelocities(context, velocities);
}
Also used : OpenMM_Vec3(simtk.openmm.OpenMM_Vec3) OpenMM_System_addConstraint(simtk.openmm.OpenMMLibrary.OpenMM_System_addConstraint) CoordRestraint(ffx.potential.nonbonded.CoordRestraint)

Example 4 with OpenMM_Vec3

use of simtk.openmm.OpenMM_Vec3 in project ffx by mjschnie.

the class ForceFieldEnergyOpenMM method fillGradients.

/**
 * Private method for internal use, so we don't have subclasses calling
 * super.energy, and this class delegating to the subclass's getGradients
 * method.
 *
 * @param g Gradient array to fill.
 * @return Gradient array.
 */
public double[] fillGradients(double[] g) {
    assert (g != null);
    int n = getNumberOfVariables();
    if (g.length < n) {
        g = new double[n];
    }
    int index = 0;
    Atom[] atoms = molecularAssembly.getAtomArray();
    int nAtoms = atoms.length;
    for (int i = 0; i < nAtoms; i++) {
        Atom a = atoms[i];
        if (a.isActive()) {
            OpenMM_Vec3 posInNm = OpenMM_Vec3Array_get(forces, i);
            /**
             * Convert OpenMM Forces in KJ/Nm into an FFX gradient in
             * Kcal/A.
             */
            double gx = -posInNm.x * OpenMM_NmPerAngstrom * OpenMM_KcalPerKJ;
            double gy = -posInNm.y * OpenMM_NmPerAngstrom * OpenMM_KcalPerKJ;
            double gz = -posInNm.z * OpenMM_NmPerAngstrom * OpenMM_KcalPerKJ;
            if (Double.isNaN(gx) || Double.isInfinite(gx) || Double.isNaN(gy) || Double.isInfinite(gy) || Double.isNaN(gz) || Double.isInfinite(gz)) {
                /*String message = format("The gradient of atom %s is (%8.3f,%8.3f,%8.3f).",
                            a.toString(), gx, gy, gz);*/
                StringBuilder sb = new StringBuilder(format("The gradient of atom %s is (%8.3f,%8.3f,%8.3f).", a.toString(), gx, gy, gz));
                double[] vals = new double[3];
                a.getVelocity(vals);
                sb.append(format("\n Velocities: %8.3g %8.3g %8.3g", vals[0], vals[1], vals[2]));
                a.getAcceleration(vals);
                sb.append(format("\n Accelerations: %8.3g %8.3g %8.3g", vals[0], vals[1], vals[2]));
                a.getPreviousAcceleration(vals);
                sb.append(format("\n Previous accelerations: %8.3g %8.3g %8.3g", vals[0], vals[1], vals[2]));
                // logger.severe(message);
                throw new EnergyException(sb.toString());
            }
            a.setXYZGradient(gx, gy, gz);
            g[index++] = gx;
            g[index++] = gy;
            g[index++] = gz;
        }
    }
    return g;
}
Also used : OpenMM_Vec3(simtk.openmm.OpenMM_Vec3) OpenMM_System_addConstraint(simtk.openmm.OpenMMLibrary.OpenMM_System_addConstraint) CoordRestraint(ffx.potential.nonbonded.CoordRestraint) Atom(ffx.potential.bonded.Atom) EnergyException(ffx.potential.utils.EnergyException)

Example 5 with OpenMM_Vec3

use of simtk.openmm.OpenMM_Vec3 in project ffx by mjschnie.

the class ForceFieldEnergyOpenMM method getOpenMMAccelerations.

/**
 * getOpenMMAccelerations takes in a PointerByReference containing the force
 * information of the context. This method creates a Vec3Array that contains
 * the three dimensional information of the forces on the atoms. This method
 * then adds these values (divided by mass, effectively turning them into
 * accelerations) to a new double array a and returns it to the method call
 *
 * @param forces
 * @param numberOfVariables
 * @param mass
 * @return
 */
public double[] getOpenMMAccelerations(PointerByReference forces, int numberOfVariables, double[] mass, double[] a) {
    assert numberOfVariables == getNumberOfVariables();
    if (a == null || a.length < numberOfVariables) {
        a = new double[numberOfVariables];
    }
    Atom[] atoms = molecularAssembly.getAtomArray();
    int nAtoms = atoms.length;
    for (int i = 0; i < nAtoms; i++) {
        int offset = i * 3;
        OpenMM_Vec3 acc = OpenMM_Vec3Array_get(forces, i);
        a[offset] = (acc.x * 10.0) / mass[i];
        a[offset + 1] = (acc.y * 10.0) / mass[i + 1];
        a[offset + 2] = (acc.z * 10.0) / mass[i + 2];
        Atom atom = atoms[i];
        double[] acceleration = { a[offset], a[offset + 1], a[offset + 2] };
        atom.setAcceleration(acceleration);
    }
    return a;
}
Also used : OpenMM_Vec3(simtk.openmm.OpenMM_Vec3) Atom(ffx.potential.bonded.Atom) OpenMM_System_addConstraint(simtk.openmm.OpenMMLibrary.OpenMM_System_addConstraint) CoordRestraint(ffx.potential.nonbonded.CoordRestraint)

Aggregations

OpenMM_Vec3 (simtk.openmm.OpenMM_Vec3)7 CoordRestraint (ffx.potential.nonbonded.CoordRestraint)6 OpenMM_System_addConstraint (simtk.openmm.OpenMMLibrary.OpenMM_System_addConstraint)6 Atom (ffx.potential.bonded.Atom)4 Crystal (ffx.crystal.Crystal)1 EnergyException (ffx.potential.utils.EnergyException)1