use of ffx.crystal.Crystal in project ffx by mjschnie.
the class XYZFilter method readPBC.
/**
* Attempt to parse the String as unit cell parameters.
*
* @param data The String to parse.
*
* @return false if the first token in the String is an integer and true
* otherwise.
*/
private static boolean readPBC(String data, MolecularAssembly activeMolecularAssembly) {
if (firstTokenIsInteger(data)) {
return false;
}
String[] tokens = data.trim().split(" +");
if (tokens != null && tokens.length == 6) {
CompositeConfiguration config = activeMolecularAssembly.getProperties();
double a = Double.parseDouble(tokens[0]);
double b = Double.parseDouble(tokens[1]);
double c = Double.parseDouble(tokens[2]);
double alpha = Double.parseDouble(tokens[3]);
double beta = Double.parseDouble(tokens[4]);
double gamma = Double.parseDouble(tokens[5]);
config.setProperty("a-axis", a);
config.setProperty("b-axis", b);
config.setProperty("c-axis", c);
config.setProperty("alpha", alpha);
config.setProperty("beta", beta);
config.setProperty("gamma", gamma);
Crystal crystal = activeMolecularAssembly.getCrystal();
if (crystal != null) {
crystal.changeUnitCellParameters(a, b, c, alpha, beta, gamma);
}
}
return true;
}
use of ffx.crystal.Crystal in project ffx by mjschnie.
the class XYZFilter method writeFile.
/**
* {@inheritDoc}
*/
@Override
public boolean writeFile(File saveFile, boolean append) {
if (saveFile == null) {
return false;
}
try {
File newFile = saveFile;
if (!append) {
newFile = version(saveFile);
}
activeMolecularAssembly.setFile(newFile);
activeMolecularAssembly.setName(newFile.getName());
FileWriter fw;
if (append && !newFile.exists()) {
fw = new FileWriter(newFile);
} else {
fw = new FileWriter(newFile, append);
}
BufferedWriter bw = new BufferedWriter(fw);
// XYZ File First Line
int numberOfAtoms = activeMolecularAssembly.getAtomList().size();
String output = format("%7d %s\n", numberOfAtoms, activeMolecularAssembly.toString());
bw.write(output);
Crystal crystal = activeMolecularAssembly.getCrystal();
if (!crystal.aperiodic()) {
Crystal uc = crystal.getUnitCell();
String params = String.format("%14.8f%14.8f%14.8f%14.8f%14.8f%14.8f\n", uc.a, uc.b, uc.c, uc.alpha, uc.beta, uc.gamma);
bw.write(params);
}
Atom a2;
StringBuilder line;
StringBuilder[] lines = new StringBuilder[numberOfAtoms];
// XYZ File Atom Lines
ArrayList<Atom> atoms = activeMolecularAssembly.getAtomList();
Vector3d offset = activeMolecularAssembly.getOffset();
for (Atom a : atoms) {
if (vdwH) {
line = new StringBuilder(String.format("%7d %3s%14.8f%14.8f%14.8f%6d", a.getIndex(), a.getAtomType().name, a.getRedX() - offset.x, a.getRedY() - offset.y, a.getRedZ() - offset.z, a.getType()));
} else {
line = new StringBuilder(String.format("%7d %3s%14.8f%14.8f%14.8f%6d", a.getIndex(), a.getAtomType().name, a.getX() - offset.x, a.getY() - offset.y, a.getZ() - offset.z, a.getType()));
}
for (Bond b : a.getBonds()) {
a2 = b.get1_2(a);
line.append(format("%8d", a2.getIndex()));
}
lines[a.getIndex() - 1] = line.append("\n");
}
try {
for (int i = 0; i < numberOfAtoms; i++) {
bw.write(lines[i].toString());
}
} catch (IOException e) {
String message = format(" Their was an unexpected error writing to %s.", getActiveMolecularSystem().toString());
logger.log(Level.WARNING, message, e);
return false;
}
bw.close();
fw.close();
} catch (IOException e) {
String message = format(" Their was an unexpected error writing to %s.", getActiveMolecularSystem().toString());
logger.log(Level.WARNING, message, e);
return false;
}
return true;
}
use of ffx.crystal.Crystal in project ffx by mjschnie.
the class ForceFieldEnergyOpenMM method addAmoebaMultipoleForce.
private void addAmoebaMultipoleForce() {
ParticleMeshEwald pme = super.getPmeNode();
if (pme == null) {
return;
}
int[][] axisAtom = pme.getAxisAtoms();
double dipoleConversion = OpenMM_NmPerAngstrom;
double quadrupoleConversion = OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom;
double polarityConversion = OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom * OpenMM_NmPerAngstrom;
double dampingFactorConversion = sqrt(OpenMM_NmPerAngstrom);
amoebaMultipoleForce = OpenMM_AmoebaMultipoleForce_create();
double polarScale = 1.0;
if (pme.getPolarizationType() != Polarization.MUTUAL) {
OpenMM_AmoebaMultipoleForce_setPolarizationType(amoebaMultipoleForce, OpenMM_AmoebaMultipoleForce_Direct);
if (pme.getPolarizationType() == Polarization.NONE) {
polarScale = 0.0;
}
} else {
ForceField forceField = molecularAssembly.getForceField();
String algorithm = forceField.getString(ForceField.ForceFieldString.SCF_ALGORITHM, "CG");
ParticleMeshEwald.SCFAlgorithm scfAlgorithm;
try {
algorithm = algorithm.replaceAll("-", "_").toUpperCase();
scfAlgorithm = ParticleMeshEwald.SCFAlgorithm.valueOf(algorithm);
} catch (Exception e) {
scfAlgorithm = ParticleMeshEwald.SCFAlgorithm.CG;
}
switch(scfAlgorithm) {
case EPT:
logger.info(" Using extrapolated perturbation theory approximation instead of full SCF calculations. Not supported in FFX reference implementation.");
OpenMM_AmoebaMultipoleForce_setPolarizationType(amoebaMultipoleForce, OpenMM_AmoebaMultipoleForce_Extrapolated);
PointerByReference exptCoefficients = OpenMM_DoubleArray_create(4);
OpenMM_DoubleArray_set(exptCoefficients, 0, -0.154);
OpenMM_DoubleArray_set(exptCoefficients, 1, 0.017);
OpenMM_DoubleArray_set(exptCoefficients, 2, 0.657);
OpenMM_DoubleArray_set(exptCoefficients, 3, 0.475);
OpenMM_AmoebaMultipoleForce_setExtrapolationCoefficients(amoebaMultipoleForce, exptCoefficients);
OpenMM_DoubleArray_destroy(exptCoefficients);
break;
case CG:
case SOR:
default:
OpenMM_AmoebaMultipoleForce_setPolarizationType(amoebaMultipoleForce, OpenMM_AmoebaMultipoleForce_Mutual);
break;
}
}
PointerByReference dipoles = OpenMM_DoubleArray_create(3);
PointerByReference quadrupoles = OpenMM_DoubleArray_create(9);
Atom[] atoms = molecularAssembly.getAtomArray();
int nAtoms = atoms.length;
for (int i = 0; i < nAtoms; i++) {
Atom atom = atoms[i];
MultipoleType multipoleType = atom.getMultipoleType();
PolarizeType polarType = atom.getPolarizeType();
/**
* Define the frame definition.
*/
int axisType = OpenMM_AmoebaMultipoleForce_NoAxisType;
switch(multipoleType.frameDefinition) {
case ZONLY:
axisType = OpenMM_AmoebaMultipoleForce_ZOnly;
break;
case ZTHENX:
axisType = OpenMM_AmoebaMultipoleForce_ZThenX;
break;
case BISECTOR:
axisType = OpenMM_AmoebaMultipoleForce_Bisector;
break;
case ZTHENBISECTOR:
axisType = OpenMM_AmoebaMultipoleForce_ZBisect;
break;
case TRISECTOR:
axisType = OpenMM_AmoebaMultipoleForce_ThreeFold;
break;
default:
break;
}
double useFactor = 1.0;
if (!atoms[i].getUse() || !atoms[i].getElectrostatics()) {
// if (!atoms[i].getUse()) {
useFactor = 0.0;
}
// Should be 1.0 at this point.
double lambdaScale = lambda;
if (!atom.applyLambda()) {
lambdaScale = 1.0;
}
useFactor *= lambdaScale;
/**
* Load local multipole coefficients.
*/
for (int j = 0; j < 3; j++) {
OpenMM_DoubleArray_set(dipoles, j, multipoleType.dipole[j] * dipoleConversion * useFactor);
}
int l = 0;
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
OpenMM_DoubleArray_set(quadrupoles, l++, multipoleType.quadrupole[j][k] * quadrupoleConversion * useFactor / 3.0);
}
}
// int zaxis = 0;
int zaxis = 1;
// int xaxis = 0;
int xaxis = 1;
// int yaxis = 0;
int yaxis = 1;
int[] refAtoms = axisAtom[i];
if (refAtoms != null) {
zaxis = refAtoms[0];
if (refAtoms.length > 1) {
xaxis = refAtoms[1];
if (refAtoms.length > 2) {
yaxis = refAtoms[2];
}
}
} else {
axisType = OpenMM_AmoebaMultipoleForce_NoAxisType;
logger.info(String.format(" Atom type %s", atom.getAtomType().toString()));
}
double charge = multipoleType.charge * useFactor;
/**
* Add the multipole.
*/
OpenMM_AmoebaMultipoleForce_addMultipole(amoebaMultipoleForce, charge, dipoles, quadrupoles, axisType, zaxis, xaxis, yaxis, polarType.thole, polarType.pdamp * dampingFactorConversion, polarType.polarizability * polarityConversion * polarScale);
}
OpenMM_DoubleArray_destroy(dipoles);
OpenMM_DoubleArray_destroy(quadrupoles);
Crystal crystal = super.getCrystal();
if (!crystal.aperiodic()) {
OpenMM_AmoebaMultipoleForce_setNonbondedMethod(amoebaMultipoleForce, OpenMM_AmoebaMultipoleForce_PME);
OpenMM_AmoebaMultipoleForce_setCutoffDistance(amoebaMultipoleForce, pme.getEwaldCutoff() * OpenMM_NmPerAngstrom);
OpenMM_AmoebaMultipoleForce_setAEwald(amoebaMultipoleForce, pme.getEwaldCoefficient() / OpenMM_NmPerAngstrom);
double ewaldTolerance = 1.0e-04;
OpenMM_AmoebaMultipoleForce_setEwaldErrorTolerance(amoebaMultipoleForce, ewaldTolerance);
PointerByReference gridDimensions = OpenMM_IntArray_create(3);
ReciprocalSpace recip = pme.getReciprocalSpace();
OpenMM_IntArray_set(gridDimensions, 0, recip.getXDim());
OpenMM_IntArray_set(gridDimensions, 1, recip.getYDim());
OpenMM_IntArray_set(gridDimensions, 2, recip.getZDim());
OpenMM_AmoebaMultipoleForce_setPmeGridDimensions(amoebaMultipoleForce, gridDimensions);
OpenMM_IntArray_destroy(gridDimensions);
} else {
OpenMM_AmoebaMultipoleForce_setNonbondedMethod(amoebaMultipoleForce, OpenMM_AmoebaMultipoleForce_NoCutoff);
}
OpenMM_AmoebaMultipoleForce_setMutualInducedMaxIterations(amoebaMultipoleForce, 500);
OpenMM_AmoebaMultipoleForce_setMutualInducedTargetEpsilon(amoebaMultipoleForce, pme.getPolarEps());
int[][] ip11 = pme.getPolarization11();
int[][] ip12 = pme.getPolarization12();
int[][] ip13 = pme.getPolarization13();
ArrayList<Integer> list12 = new ArrayList<>();
ArrayList<Integer> list13 = new ArrayList<>();
ArrayList<Integer> list14 = new ArrayList<>();
PointerByReference covalentMap = OpenMM_IntArray_create(0);
for (int i = 0; i < nAtoms; i++) {
Atom ai = atoms[i];
list12.clear();
list13.clear();
list14.clear();
for (Bond bond : ai.getBonds()) {
int index = bond.get1_2(ai).getIndex() - 1;
OpenMM_IntArray_append(covalentMap, index);
list12.add(index);
}
OpenMM_AmoebaMultipoleForce_setCovalentMap(amoebaMultipoleForce, i, OpenMM_AmoebaMultipoleForce_Covalent12, covalentMap);
OpenMM_IntArray_resize(covalentMap, 0);
for (Angle angle : ai.getAngles()) {
Atom ak = angle.get1_3(ai);
if (ak != null) {
int index = ak.getIndex() - 1;
if (!list12.contains(index)) {
list13.add(index);
OpenMM_IntArray_append(covalentMap, index);
}
}
}
OpenMM_AmoebaMultipoleForce_setCovalentMap(amoebaMultipoleForce, i, OpenMM_AmoebaMultipoleForce_Covalent13, covalentMap);
OpenMM_IntArray_resize(covalentMap, 0);
for (Torsion torsion : ai.getTorsions()) {
Atom ak = torsion.get1_4(ai);
if (ak != null) {
int index = ak.getIndex() - 1;
if (!list12.contains(index) && !list13.contains(index)) {
list14.add(index);
OpenMM_IntArray_append(covalentMap, index);
}
}
}
OpenMM_AmoebaMultipoleForce_setCovalentMap(amoebaMultipoleForce, i, OpenMM_AmoebaMultipoleForce_Covalent14, covalentMap);
OpenMM_IntArray_resize(covalentMap, 0);
for (Atom ak : ai.get1_5s()) {
int index = ak.getIndex() - 1;
if (!list12.contains(index) && !list13.contains(index) && !list14.contains(index)) {
OpenMM_IntArray_append(covalentMap, index);
}
}
OpenMM_AmoebaMultipoleForce_setCovalentMap(amoebaMultipoleForce, i, OpenMM_AmoebaMultipoleForce_Covalent15, covalentMap);
OpenMM_IntArray_resize(covalentMap, 0);
for (int j = 0; j < ip11[i].length; j++) {
OpenMM_IntArray_append(covalentMap, ip11[i][j]);
}
OpenMM_AmoebaMultipoleForce_setCovalentMap(amoebaMultipoleForce, i, OpenMM_AmoebaMultipoleForce_PolarizationCovalent11, covalentMap);
OpenMM_IntArray_resize(covalentMap, 0);
// for (int j = 0; j < ip12[i].length; j++) {
// OpenMM_IntArray_append(covalentMap, ip12[i][j]);
// }
// OpenMM_AmoebaMultipoleForce_setCovalentMap(amoebaMultipoleForce, i,
// OpenMM_AmoebaMultipoleForce_PolarizationCovalent12, covalentMap);
// OpenMM_IntArray_resize(covalentMap, 0);
//
// for (int j = 0; j < ip13[i].length; j++) {
// OpenMM_IntArray_append(covalentMap, ip13[i][j]);
// }
// OpenMM_AmoebaMultipoleForce_setCovalentMap(amoebaMultipoleForce, i,
// OpenMM_AmoebaMultipoleForce_PolarizationCovalent13, covalentMap);
// OpenMM_IntArray_resize(covalentMap, 0);
//
// OpenMM_AmoebaMultipoleForce_setCovalentMap(amoebaMultipoleForce, i,
// OpenMM_AmoebaMultipoleForce_PolarizationCovalent14, covalentMap);
}
OpenMM_IntArray_destroy(covalentMap);
OpenMM_System_addForce(system, amoebaMultipoleForce);
OpenMM_Force_setForceGroup(amoebaMultipoleForce, 1);
logger.log(Level.INFO, " Added polarizable multipole force.");
GeneralizedKirkwood gk = super.getGK();
if (gk != null) {
addGKForce();
}
}
use of ffx.crystal.Crystal in project ffx by mjschnie.
the class ForceFieldEnergyOpenMM method setDefaultPeriodicBoxVectors.
private void setDefaultPeriodicBoxVectors() {
OpenMM_Vec3 a = new OpenMM_Vec3();
OpenMM_Vec3 b = new OpenMM_Vec3();
OpenMM_Vec3 c = new OpenMM_Vec3();
Crystal crystal = super.getCrystal();
if (!crystal.aperiodic()) {
a.x = crystal.a * OpenMM_NmPerAngstrom;
a.y = 0.0 * OpenMM_NmPerAngstrom;
a.z = 0.0 * OpenMM_NmPerAngstrom;
b.x = 0.0 * OpenMM_NmPerAngstrom;
b.y = crystal.b * OpenMM_NmPerAngstrom;
b.z = 0.0 * OpenMM_NmPerAngstrom;
c.x = 0.0 * OpenMM_NmPerAngstrom;
c.y = 0.0 * OpenMM_NmPerAngstrom;
c.z = crystal.c * OpenMM_NmPerAngstrom;
OpenMM_System_setDefaultPeriodicBoxVectors(system, a, b, c);
}
}
use of ffx.crystal.Crystal in project ffx by mjschnie.
the class ForceFieldEnergyOpenMM method addFixedChargeNonBondedForce.
/**
* Uses arithmetic mean to define sigma and geometric mean for epsilon.
*/
private void addFixedChargeNonBondedForce() {
VanDerWaals vdW = super.getVdwNode();
if (vdW == null) {
return;
}
/**
* Only 6-12 LJ with arithmetic mean to define sigma and geometric mean
* for epsilon is supported.
*/
VanDerWaalsForm vdwForm = vdW.getVDWForm();
if (vdwForm.vdwType != LENNARD_JONES || vdwForm.radiusRule != ARITHMETIC || vdwForm.epsilonRule != GEOMETRIC) {
logger.info(format(" VDW Type: %s", vdwForm.vdwType));
logger.info(format(" VDW Radius Rule: %s", vdwForm.radiusRule));
logger.info(format(" VDW Epsilon Rule: %s", vdwForm.epsilonRule));
logger.log(Level.SEVERE, String.format(" Unsuppporterd van der Waals functional form."));
return;
}
fixedChargeNonBondedForce = OpenMM_NonbondedForce_create();
/**
* OpenMM vdW force requires a diameter (i.e. not radius).
*/
double radScale = 1.0;
if (vdwForm.radiusSize == RADIUS) {
radScale = 2.0;
}
/**
* OpenMM vdw force requires atomic sigma values (i.e. not r-min).
*/
if (vdwForm.radiusType == R_MIN) {
radScale /= 1.122462048309372981;
}
/**
* Add particles.
*/
Atom[] atoms = molecularAssembly.getAtomArray();
int nAtoms = atoms.length;
for (int i = 0; i < nAtoms; i++) {
Atom atom = atoms[i];
VDWType vdwType = atom.getVDWType();
double sigma = OpenMM_NmPerAngstrom * vdwType.radius * radScale;
double eps = OpenMM_KJPerKcal * vdwType.wellDepth;
double charge = 0.0;
MultipoleType multipoleType = atom.getMultipoleType();
if (multipoleType != null && atoms[i].getElectrostatics()) {
charge = multipoleType.charge;
}
OpenMM_NonbondedForce_addParticle(fixedChargeNonBondedForce, charge, sigma, eps);
}
/**
* Define 1-4 scale factors.
*/
double lj14Scale = vdwForm.getScale14();
double coulomb14Scale = 1.0 / 1.2;
ParticleMeshEwald pme = super.getPmeNode();
Bond[] bonds = super.getBonds();
if (bonds != null && bonds.length > 0) {
int nBonds = bonds.length;
PointerByReference bondArray;
bondArray = OpenMM_BondArray_create(0);
for (int i = 0; i < nBonds; i++) {
Bond bond = bonds[i];
int i1 = bond.getAtom(0).getXyzIndex() - 1;
int i2 = bond.getAtom(1).getXyzIndex() - 1;
OpenMM_BondArray_append(bondArray, i1, i2);
}
if (pme != null) {
coulomb14Scale = pme.getScale14();
}
OpenMM_NonbondedForce_createExceptionsFromBonds(fixedChargeNonBondedForce, bondArray, coulomb14Scale, lj14Scale);
OpenMM_BondArray_destroy(bondArray);
int num = OpenMM_NonbondedForce_getNumExceptions(fixedChargeNonBondedForce);
chargeExclusion = new boolean[num];
vdWExclusion = new boolean[num];
exceptionChargeProd = new double[num];
exceptionEps = new double[num];
IntByReference particle1 = new IntByReference();
IntByReference particle2 = new IntByReference();
DoubleByReference chargeProd = new DoubleByReference();
DoubleByReference sigma = new DoubleByReference();
DoubleByReference eps = new DoubleByReference();
for (int i = 0; i < num; i++) {
OpenMM_NonbondedForce_getExceptionParameters(fixedChargeNonBondedForce, i, particle1, particle2, chargeProd, sigma, eps);
if (abs(chargeProd.getValue()) > 0.0) {
chargeExclusion[i] = false;
exceptionChargeProd[i] = chargeProd.getValue();
} else {
exceptionChargeProd[i] = 0.0;
chargeExclusion[i] = true;
}
if (abs(eps.getValue()) > 0.0) {
vdWExclusion[i] = false;
exceptionEps[i] = eps.getValue();
} else {
vdWExclusion[i] = true;
exceptionEps[i] = 0.0;
}
}
}
Crystal crystal = super.getCrystal();
if (crystal.aperiodic()) {
OpenMM_NonbondedForce_setNonbondedMethod(fixedChargeNonBondedForce, OpenMM_NonbondedForce_NonbondedMethod.OpenMM_NonbondedForce_NoCutoff);
} else {
OpenMM_NonbondedForce_setNonbondedMethod(fixedChargeNonBondedForce, OpenMM_NonbondedForce_NonbondedMethod.OpenMM_NonbondedForce_PME);
if (pme != null) {
// Units of the Ewald coefficient are A^-1; Multiply by AngstromsPerNM to convert to (Nm^-1).
double aEwald = OpenMM_AngstromsPerNm * pme.getEwaldCoefficient();
int nx = pme.getReciprocalSpace().getXDim();
int ny = pme.getReciprocalSpace().getYDim();
int nz = pme.getReciprocalSpace().getZDim();
OpenMM_NonbondedForce_setPMEParameters(fixedChargeNonBondedForce, aEwald, nx, ny, nz);
}
NonbondedCutoff nonbondedCutoff = vdW.getNonbondedCutoff();
double off = nonbondedCutoff.off;
double cut = nonbondedCutoff.cut;
OpenMM_NonbondedForce_setCutoffDistance(fixedChargeNonBondedForce, OpenMM_NmPerAngstrom * off);
OpenMM_NonbondedForce_setUseSwitchingFunction(fixedChargeNonBondedForce, OpenMM_True);
if (cut == off) {
logger.warning(" OpenMM does not properly handle cutoffs where cut == off!");
if (cut == Double.MAX_VALUE || cut == Double.POSITIVE_INFINITY) {
logger.info(" Detected infinite or max-value cutoff; setting cut to 1E+40 for OpenMM.");
cut = 1E40;
} else {
logger.info(String.format(" Detected cut %8.4g == off %8.4g; scaling cut to 0.99 of off for OpenMM.", cut, off));
cut *= 0.99;
}
}
OpenMM_NonbondedForce_setSwitchingDistance(fixedChargeNonBondedForce, OpenMM_NmPerAngstrom * cut);
}
OpenMM_NonbondedForce_setUseDispersionCorrection(fixedChargeNonBondedForce, OpenMM_False);
// OpenMM_Force_setForceGroup(fixedChargeNonBondedForce, 1);
OpenMM_System_addForce(system, fixedChargeNonBondedForce);
logger.log(Level.INFO, String.format(" Added fixed charge non-bonded force."));
GeneralizedKirkwood gk = super.getGK();
if (gk != null) {
addCustomGBForce();
}
}
Aggregations