Python wrapping code for gmxapi mdrun bindings.
[alexxy/gromacs.git] / python_packaging / src / gmxapi / simulation / mdrun.py
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2019, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34
35 """mdrun operation module
36
37 The mdrun operation (in its first draft) conforms to the user-level API, but does
38 not use the Python Context resource manager. It uses either the legacy 0.0.7
39 Context or its own Context, also implemented in this module.
40 """
41
42 import typing
43
44 from gmxapi import exceptions
45 from gmxapi.operation import AbstractOperation
46 from gmxapi.operation import function_wrapper
47 # The following imports are not marked as public API.
48 # TODO: Resolve public API and exposure.
49 from . import workflow
50
51
52 def mdrun_dispatcher(context, *, input, label: str = None, **kwargs) -> typing.Type['AbstractOperation']:
53     """Dispatch to an appropriate director based on the context and input.
54
55     Runs appropriate director code to set up an operation, returning a handle to
56     a simulation operation node.
57
58     Arguments:
59         context: Execution context in which to attempt to instantiate an operation from input
60         input: operation input. Argument type is context dependent.
61         label: human readable tag for the work node to be created.
62
63     Additional key word arguments are passed on to the dispatched factory.
64
65     """
66     # TODO: This is a legacy import that should be updated.
67     from .context import get_context
68     if label is not None:
69         raise exceptions.NotImplementedError('sorry... no labels yet')
70     try:
71         legacy_context = get_context(work=input)
72     except Exception:
73         legacy_context = None
74
75     def run_session():
76         with legacy_context as session:
77             session.run()
78         return True
79
80     if context is not None and context is legacy_context:
81         helper = function_wrapper(output={'data': bool})(run_session)
82         return helper(**kwargs)
83     else:
84         raise exceptions.ValueError('Could not dispatch MD input {} with context {}'.format(input, legacy_context))
85
86
87 def mdrun(input=None) -> typing.Type['AbstractOperation']:
88     """MD simulation operation.
89
90     Arguments:
91         input : valid simulation input
92
93     Returns:
94         runnable operation to perform the specified simulation
95
96     The returned object has a `run()` method to launch the simulation.
97     Otherwise, this operation does not yet support the gmxapi data flow model.
98
99     `input` may be a TPR file name.
100
101     Note:
102         New function names will be appearing to handle tasks that are separate
103
104         "simulate" is plausibly a dispatcher or base class for various tasks
105         dispatched by mdrun. Specific work factories are likely "minimize,"
106         "test_particle_insertion," "legacy_simulation" (do_md), or "simulation"
107         composition (which may be leap-frog, vv, and other algorithms)
108     """
109     # If input looks like classic filename input, use the gmxapi 0.0.7
110     # implementation.
111     from .context import get_context
112     try:
113         work = workflow.from_tpr(input)
114         assert work is not None
115         work_context = get_context(work)
116         assert work_context is not None
117     except exceptions.UsageError:
118         # Input was not gmxapi 0.0.7 input.
119         work = None
120         work_context = None
121
122     # TODO: Inspect input to determine context.
123     kwargs = {}
124     handle = mdrun_dispatcher(context=work_context, input=work, label=None, **kwargs)
125
126     return handle