1 package ffx.algorithms.optimize;
2
3 import ffx.algorithms.AlgorithmListener;
4 import ffx.algorithms.dynamics.MolecularDynamics;
5 import ffx.algorithms.dynamics.PhReplicaExchange;
6 import ffx.algorithms.dynamics.integrators.IntegratorEnum;
7 import ffx.algorithms.dynamics.thermostats.ThermostatEnum;
8 import ffx.algorithms.misc.AlgorithmsTest;
9 import ffx.potential.ForceFieldEnergy;
10 import ffx.potential.MolecularAssembly;
11 import ffx.potential.extended.ExtendedSystem;
12 import ffx.potential.utils.PotentialsUtils;
13 import org.junit.Test;
14
15 import java.io.File;
16
17 import static org.junit.Assert.*;
18
19 public class PhRepexTest extends AlgorithmsTest {
20
21 @Test
22 public void testPhRepexRestarts() {
23 System.setProperty("ffx.log", "severe");
24 String structure = getResourcePath("PhRepexTestFiles/validRestart/LYS_penta.pdb");
25 PotentialsUtils potentialUtils = new PotentialsUtils();
26 MolecularAssembly molecularAssembly = potentialUtils.openQuietly(structure);
27 ForceFieldEnergy potential = molecularAssembly.getPotentialEnergy();
28 ExtendedSystem esvSystem = new ExtendedSystem(molecularAssembly, 7, null);
29 potential.attachExtendedSystem(esvSystem);
30 double[] pHLadder = new double[]{8.4, 11.4, 11.9};
31 double[] x = new double[potential.getNumberOfVariables()];
32 potential.getCoordinates(x);
33 File structureFile = new File(structure);
34 int rank = 0;
35 final String newMolAssemblyFile = structureFile.getParent() + File.separator + rank +
36 File.separator + structureFile.getName();
37 molecularAssembly.setFile(new File(newMolAssemblyFile));
38 AlgorithmListener algorithmListener = new AlgorithmListener() {
39 @Override
40 public boolean algorithmUpdate(MolecularAssembly active) {
41 return true;
42 }
43 };
44 MolecularDynamics molecularDynamics = MolecularDynamics.dynamicsFactory(molecularAssembly, potential, algorithmListener,
45 ThermostatEnum.ADIABATIC, IntegratorEnum.STOCHASTIC);
46 try {
47
48 PhReplicaExchange pHReplicaExchange = new PhReplicaExchange(molecularDynamics, structureFile, 7, pHLadder,
49 298, esvSystem, x, 3);
50
51 structureFile = new File(getResourcePath("PhRepexTestFiles/validAfterPrimaryPhFail/LYS_penta.pdb"));
52 pHReplicaExchange = new PhReplicaExchange(molecularDynamics, structureFile, 7, pHLadder,
53 298, esvSystem, x, 3);
54 structureFile = new File(getResourcePath("PhRepexTestFiles/validAfterPrimaryCountFail/LYS_penta.pdb"));
55 pHReplicaExchange = new PhReplicaExchange(molecularDynamics, structureFile, 7, pHLadder,
56 298, esvSystem, x, 3);
57 } catch (Exception e) {
58
59 fail("Unexpected exception thrown from " + structureFile + ": " + e.getMessage());
60 }
61 try{
62 structureFile = new File(getResourcePath("PhRepexTestFiles/invalidAfterPhFail/LYS_penta.pdb"));
63 PhReplicaExchange pHReplicaExchange = new PhReplicaExchange(molecularDynamics, structureFile, 7, pHLadder,
64 298, esvSystem, x, 3);
65 fail("Expected exception not thrown from " + structureFile);
66 } catch (Exception ignored) {}
67 try{
68 structureFile = new File(getResourcePath("PhRepexTestFiles/invalidAfterCountFail/LYS_penta.pdb"));
69 PhReplicaExchange pHReplicaExchange = new PhReplicaExchange(molecularDynamics, structureFile, 7, pHLadder,
70 298, esvSystem, x, 3);
71 fail("Expected exception not thrown from " + structureFile);
72 } catch (Exception ignored) {}
73 }
74
75 @Test
76 public void testPhRepexExchangeValidity() {
77 System.setProperty("ffx.log", "severe");
78 String structure = getResourcePath("PhRepexTestFiles/validRestart/LYS_penta.pdb");
79 PotentialsUtils potentialUtils = new PotentialsUtils();
80 MolecularAssembly molecularAssembly = potentialUtils.openQuietly(structure);
81 ForceFieldEnergy potential = molecularAssembly.getPotentialEnergy();
82 ExtendedSystem esvSystem = new ExtendedSystem(molecularAssembly, 7, null);
83 potential.attachExtendedSystem(esvSystem);
84 double[] pHLadder = new double[]{8.4, 8.9, 9.4, 9.9, 10.4, 10.9, 11.4, 11.9};
85 double[] x = new double[potential.getNumberOfVariables()];
86 potential.getCoordinates(x);
87 File structureFile = new File(structure);
88 int rank = 0;
89 final String newMolAssemblyFile = structureFile.getParent() + File.separator + rank +
90 File.separator + structureFile.getName();
91 molecularAssembly.setFile(new File(newMolAssemblyFile));
92 AlgorithmListener algorithmListener = new AlgorithmListener() {
93 @Override
94 public boolean algorithmUpdate(MolecularAssembly active) {
95 return true;
96 }
97 };
98 MolecularDynamics molecularDynamics = MolecularDynamics.dynamicsFactory(molecularAssembly, potential, algorithmListener,
99 ThermostatEnum.ADIABATIC, IntegratorEnum.STOCHASTIC);
100 try {
101 PhReplicaExchange pHReplicaExchange = new PhReplicaExchange(molecularDynamics, structureFile, 7, pHLadder,
102 298, esvSystem, x, 8);
103 int[] pHMap = pHReplicaExchange.setTestingParametersAndExchangeOnce();
104 int[] expected = new int[]{3, 0, 1, 2, 7, 4, 5, 6,
105 1, 2, 3, 0, 5, 6, 7, 4};
106 assertArrayEquals(expected, pHMap);
107 } catch(Exception e) {
108 fail("Unexpected exception thrown from " + structureFile + ": " + e.getMessage());
109 }
110 }
111 }