View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.openmm;
39  
40  import com.sun.jna.Memory;
41  import com.sun.jna.Pointer;
42  import com.sun.jna.ptr.PointerByReference;
43  import edu.uiowa.jopenmm.OpenMMLibrary;
44  
45  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Platform_destroy;
46  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Platform_getName;
47  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Platform_getNumPlatforms;
48  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Platform_getOpenMMVersion;
49  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Platform_getPlatformByName;
50  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Platform_getPluginLoadFailures;
51  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Platform_getSpeed;
52  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Platform_loadPluginsFromDirectory;
53  
54  /**
55   * A Platform defines an implementation of all the kernels needed to perform some calculation.
56   * More precisely, a Platform object acts as a registry for a set of KernelFactory
57   * objects which together implement the kernels.  The Platform class, in turn, provides a
58   * static registry of all available Platform objects.
59   */
60  public class Platform {
61  
62    /**
63     * OpenMM Platform pointer.
64     */
65    private PointerByReference pointer;
66  
67    /**
68     * OpenMM Platform constructor.
69     *
70     * @param platformName The name of the OpenMM Platform.
71     */
72    public Platform(String platformName) {
73      pointer = OpenMM_Platform_getPlatformByName(platformName);
74    }
75  
76    /**
77     * Get the OpenMM Platform pointer.
78     *
79     * @return The OpenMM Platform pointer.
80     */
81    public PointerByReference getPointer() {
82      return pointer;
83    }
84  
85    /**
86     * Get the name of the OpenMM Platform.
87     *
88     * @return The name of the OpenMM Platform.
89     */
90    public String getName() {
91      Pointer name = OpenMM_Platform_getName(pointer);
92      return name.getString(0);
93    }
94  
95    /**
96     * Get an estimate of how fast this Platform class is.
97     *
98     * @return The speed of the OpenMM Platform.
99     */
100   public double getSpeed() {
101     return OpenMM_Platform_getSpeed(pointer);
102   }
103 
104   /**
105    * Set an OpenMM Platform property.
106    */
107   public void setPropertyDefaultValue(String propertyName, String defaultValue) {
108     Pointer name = pointerForString(propertyName);
109     Pointer value = pointerForString(defaultValue);
110     OpenMMLibrary.OpenMM_Platform_setPropertyDefaultValue(pointer, name, value);
111   }
112 
113   /**
114    * Get the number of OpenMM Platforms.
115    *
116    * @return The number of OpenMM Platforms.
117    */
118   public static int getNumPlatforms() {
119     return OpenMM_Platform_getNumPlatforms();
120   }
121 
122   /**
123    * Get the OpenMM version.
124    *
125    * @return The version of OpenMM.
126    */
127   public static String getOpenMMVersion() {
128     Pointer version = OpenMM_Platform_getOpenMMVersion();
129     return version.getString(0);
130   }
131 
132   /**
133    * Load plugins from a directory.
134    *
135    * @param directory The directory to load plugins from.
136    * @return The OpenMMStringArray of plugins loaded.
137    */
138   public static StringArray loadPluginsFromDirectory(String directory) {
139     return new StringArray(OpenMM_Platform_loadPluginsFromDirectory(directory));
140   }
141 
142   /**
143    * Get the plugin load failures.
144    *
145    * @return The OpenMMStringArray of plugin load failures.
146    */
147   public static StringArray getPluginLoadFailures() {
148     return new StringArray(OpenMM_Platform_getPluginLoadFailures());
149   }
150 
151   /**
152    * Destroy the OpenMM Platform instance.
153    */
154   public void destroy() {
155     if (pointer != null) {
156       OpenMM_Platform_destroy(pointer);
157       pointer = null;
158     }
159   }
160 
161   /**
162    * Create a JNA Pointer to a String.
163    *
164    * @param string WARNING: assumes ascii-only string
165    * @return pointer.
166    */
167   private static Pointer pointerForString(String string) {
168     Pointer pointer = new Memory(string.length() + 1);
169     pointer.setString(0, string);
170     return pointer;
171   }
172 
173 }