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