File: genericfileio.py

package info (click to toggle)
python-ase 3.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 15,448 kB
  • sloc: python: 144,945; xml: 2,728; makefile: 113; javascript: 47
file content (346 lines) | stat: -rw-r--r-- 10,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import shlex
from abc import ABC, abstractmethod
from contextlib import ExitStack
from os import PathLike
from pathlib import Path
from typing import Any, Iterable, List, Mapping, Optional, Set

from ase.calculators.abc import GetOutputsMixin
from ase.calculators.calculator import (
    BadConfiguration,
    BaseCalculator,
    _validate_command,
)
from ase.config import cfg as _cfg

link_calculator_docs = (
    "https://wiki.fysik.dtu.dk/ase/ase/calculators/"
    "calculators.html#calculator-configuration"
)


class BaseProfile(ABC):
    configvars: Set[str] = set()

    def __init__(self, command):
        self.command = _validate_command(command)

    @property
    def _split_command(self):
        return shlex.split(self.command)

    def get_command(self, inputfile, calc_command=None) -> List[str]:
        """
        Get the command to run. This should be a list of strings.

        Parameters
        ----------
        inputfile : str
        calc_command: list[str]: calculator command (used for sockets)

        Returns
        -------
        list of str
            The command to run.
        """
        if calc_command is None:
            calc_command = self.get_calculator_command(inputfile)
        return [*self._split_command, *calc_command]

    @abstractmethod
    def get_calculator_command(self, inputfile):
        """
        The calculator specific command as a list of strings.

        Parameters
        ----------
        inputfile : str

        Returns
        -------
        list of str
            The command to run.
        """

    def run(
        self, directory: Path, inputfile: Optional[str],
        outputfile: str, errorfile: Optional[str] = None,
        append: bool = False
    ) -> None:
        """
        Run the command in the given directory.

        Parameters
        ----------
        directory : pathlib.Path
            The directory to run the command in.
        inputfile : Optional[str]
            The name of the input file.
        outputfile : str
            The name of the output file.
        errorfile: Optional[str]
            the stderror file
        append: bool
            if True then use append mode
        """

        import os
        from subprocess import check_call

        argv_command = self.get_command(inputfile)
        mode = 'wb' if not append else 'ab'

        with ExitStack() as stack:
            output_path = directory / outputfile
            fd_out = stack.enter_context(open(output_path, mode))
            if errorfile is not None:
                error_path = directory / errorfile
                fd_err = stack.enter_context(open(error_path, mode))
            else:
                fd_err = None
            check_call(
                argv_command,
                cwd=directory,
                stdout=fd_out,
                stderr=fd_err,
                env=os.environ,
            )

    @abstractmethod
    def version(self):
        """Get the version of the code.

        Returns
        -------
        str
            The version of the code.
        """

    @classmethod
    def from_config(cls, cfg, section_name):
        """Create a profile from a configuration file.

        Parameters
        ----------
        cfg : ase.config.Config
            The configuration object.
        section_name : str
            The name of the section in the configuration file. E.g. the name
            of the template that this profile is for.

        Returns
        -------
        BaseProfile
            The profile object.
        """
        section = cfg.parser[section_name]
        command = section['command']

        kwargs = {
            varname: section[varname]
            for varname in cls.configvars if varname in section
        }

        try:
            return cls(command=command, **kwargs)
        except TypeError as err:
            raise BadConfiguration(*err.args)


def read_stdout(args, createfile=None):
    """Run command in tempdir and return standard output.

    Helper function for getting version numbers of DFT codes.
    Most DFT codes don't implement a --version flag, so in order to
    determine the code version, we just run the code until it prints
    a version number."""
    import tempfile
    from subprocess import PIPE, Popen

    with tempfile.TemporaryDirectory() as directory:
        if createfile is not None:
            path = Path(directory) / createfile
            path.touch()
        proc = Popen(
            args,
            stdout=PIPE,
            stderr=PIPE,
            stdin=PIPE,
            cwd=directory,
            encoding='utf-8',  # Make this a parameter if any non-utf8/ascii
        )
        stdout, _ = proc.communicate()
        # Exit code will be != 0 because there isn't an input file
    return stdout


class CalculatorTemplate(ABC):
    def __init__(self, name: str, implemented_properties: Iterable[str]):
        self.name = name
        self.implemented_properties = frozenset(implemented_properties)

    @abstractmethod
    def write_input(self, profile, directory, atoms, parameters, properties):
        ...

    @abstractmethod
    def execute(self, directory, profile):
        ...

    @abstractmethod
    def read_results(self, directory: PathLike) -> Mapping[str, Any]:
        ...

    @abstractmethod
    def load_profile(self, cfg):
        ...

    def socketio_calculator(
        self,
        profile,
        parameters,
        directory,
        # We may need quite a few socket kwargs here
        # if we want to expose all the timeout etc. from
        # SocketIOCalculator.
        unixsocket=None,
        port=None,
    ):
        import os
        from subprocess import Popen

        from ase.calculators.socketio import SocketIOCalculator

        if port and unixsocket:
            raise TypeError(
                'For the socketio_calculator only a UNIX '
                '(unixsocket) or INET (port) socket can be used'
                ' not both.'
            )

        if not port and not unixsocket:
            raise TypeError(
                'For the socketio_calculator either a '
                'UNIX (unixsocket) or INET (port) socket '
                'must be used'
            )

        if not (
            hasattr(self, 'socketio_argv')
            and hasattr(self, 'socketio_parameters')
        ):
            raise TypeError(
                f'Template {self} does not implement mandatory '
                'socketio_argv() and socketio_parameters()'
            )

        # XXX need socketio ABC or something
        argv = profile.get_command(
            inputfile=None,
            calc_command=self.socketio_argv(profile, unixsocket, port)
        )
        parameters = {
            **self.socketio_parameters(unixsocket, port),
            **parameters,
        }

        # Not so elegant that socket args are passed to this function
        # via socketiocalculator when we could make a closure right here.
        def launch(atoms, properties, port, unixsocket):
            directory.mkdir(exist_ok=True, parents=True)

            self.write_input(
                atoms=atoms,
                profile=profile,
                parameters=parameters,
                properties=properties,
                directory=directory,
            )

            with open(directory / self.outputname, 'w') as out_fd:
                return Popen(argv, stdout=out_fd, cwd=directory, env=os.environ)

        return SocketIOCalculator(
            launch_client=launch, unixsocket=unixsocket, port=port
        )


class GenericFileIOCalculator(BaseCalculator, GetOutputsMixin):
    cfg = _cfg

    def __init__(
        self,
        *,
        template,
        profile,
        directory,
        parameters=None,
    ):
        self.template = template
        if profile is None:
            if template.name not in self.cfg.parser:
                raise BadConfiguration(
                    f"No configuration of '{template.name}'. "
                    f"See '{link_calculator_docs}'"
                )
            try:
                profile = template.load_profile(self.cfg)
            except Exception as err:
                configvars = self.cfg.as_dict()
                raise BadConfiguration(
                    f'Failed to load section [{template.name}] '
                    f'from configuration: {configvars}'
                ) from err

        self.profile = profile

        # Maybe we should allow directory to be a factory, so
        # calculators e.g. produce new directories on demand.
        self.directory = Path(directory)
        super().__init__(parameters)

    def set(self, *args, **kwargs):
        raise RuntimeError(
            'No setting parameters for now, please.  '
            'Just create new calculators.'
        )

    def __repr__(self):
        return f'{type(self).__name__}({self.template.name})'

    @property
    def implemented_properties(self):
        return self.template.implemented_properties

    @property
    def name(self):
        return self.template.name

    def write_inputfiles(self, atoms, properties):
        # SocketIOCalculators like to write inputfiles
        # without calculating.
        self.directory.mkdir(exist_ok=True, parents=True)
        self.template.write_input(
            profile=self.profile,
            atoms=atoms,
            parameters=self.parameters,
            properties=properties,
            directory=self.directory,
        )

    def calculate(self, atoms, properties, system_changes):
        self.write_inputfiles(atoms, properties)
        self.template.execute(self.directory, self.profile)
        self.results = self.template.read_results(self.directory)
        # XXX Return something useful?

    def _outputmixin_get_results(self):
        return self.results

    def socketio(self, **socketkwargs):
        return self.template.socketio_calculator(
            directory=self.directory,
            parameters=self.parameters,
            profile=self.profile,
            **socketkwargs,
        )